C# DataBinding List<string> to DataGridColumn
I defined a example class below:
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; }
}
}
When I databind this to a DataGridView, everything except the Test is bound correctly. The checkbox for Active is also created automaticly in the datagridview.
Is there way to tell the databinding that the List needs to be bound as a ComboBox?
Thanks for the time.
I am assuming you are trying to bind the list to a Column Cell that is flagged as a ComboBox.
You will need to specify the column in the data grid "Columns" collection, and set data binding for the column. If you have already done that, please post a more detailed description.
Try this, use a template field to add the combo box and then add the data to is in the OnRowDataBound Event.
<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>
in the code behind:
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();
}
精彩评论