【问题标题】:C# DataBinding List<string> to DataGridColumnC# DataBinding List<string> 到 DataGridColumn
【发布时间】:2011-03-25 20:27:34
【问题描述】:

我在下面定义了一个示例类:

class Shops : INotifyPropertyChanged
{
    private int _productId;
        private string _productName;
        private List<string> _test;
    private bool _active;

    public int ProductId
        {
            get { return _productId; }
            set { _productId = value; }
        }

    public string ProductName
        {
            get { return _productName; }
            set { _productName = value; }
        }

        public List<string> Test
        {
            get { return _test; }
            set { _test = value; }
        }

        public bool Active
        {
            get { return _active; }
            set { _active = value; }
        }
}

当我将此数据绑定到 DataGridView 时,除 Test 之外的所有内容都已正确绑定。在 datagridview 中也会自动创建 Active 复选框。

有没有办法告诉数据绑定 List 需要绑定为 ComboBox?

感谢您的时间。

【问题讨论】:

    标签: c# data-binding datagridview


    【解决方案1】:

    我假设您正在尝试将列表绑定到标记为 ComboBox 的列单元格。

    您需要在数据网格“列”集合中指定列,并为该列设置数据绑定。如果您已经这样做了,请发布更详细的说明。

    【讨论】:

    • 绑定在运行时。所以我不是自己添加列。绑定正在处理这个问题。
    • @Shift - 您可以在 DataGridView 的 columns 属性中仅指定一个需要特殊行为的特殊列,所有其他列将动态构建和绑定。只要您为该一列正确设置了绑定,它的行为就应该如您所愿。顺便说一句:这是 Winforms 还是 Webforms?
    【解决方案2】:

    试试这个,使用模板字段添加组合框,然后在 OnRowDataBound 事件中添加数据。

    <asp:GridView ID="GridView1" runat="server"
                OnRowDataBound="bindCombobox">
    
                <Columns>
    
                //other columns
    
                <asp:TemplateField HeaderText="status" >
                <ItemTemplate>
                    <asp:DropDownList ID="comboBox1" runat="server"></asp:DropDownList>
                </ItemTemplate>
                </asp:TemplateField>
    
    
                </Columns>
    
    </asp:GridView>
    

    在后面的代码中:

    public void bindComboBox(object sender, GridViewRowEventArgs e)
            {
    
                if (e.Row.RowType != DataControlRowType.DataRow)
                {
                    return;
                }
    
                DropDownList ddlist = (DropDownList)e.Row.FindControl("comboBox1");
                ddlist.AppendDataBoundItems = true;
                ddlist.DataSource = Test;  //insert your list here
                ddlist.DataBind();
    
            }
    

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 2013-11-03
      • 2012-12-26
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2016-09-24
      相关资源
      最近更新 更多