我在winforms中有一个DataGridView,它包含一个组合框列。 gridview以编程方式填充,并且在开始时为空。 “list”是包含自定义对象的列表。 Value1 / 2/3对每一行都是唯一的。
int[] array = new[] {value1,value2,value3}
for (int i = 0; i < list.Count; i++)
{
DataRow newrow = MyDataTable.NewRow();
newrow["idColumn"] = list[i].Customer_id;
newrow["dateoforderColumn"] = list[i].Customer_date;
newrow["commColumn"] = list[i].Customer_comment;
// This is what I want, and can't get to work.
newrow["comboColumn"] = array;
// I have also tried (newrow["comboColumn"] as DataGridViewComboColumn).DataSource
// but it didn't work either.
MyDataTable.Rows.Add(newrow);
}
如何填充新创建的组合框?另外,我需要在for循环中执行它,因为我需要索引来获取数据。
答案 0 :(得分:1)
从代码的外观来看,您只需要设置一个值数组。在这种情况下,您只需要为列设置一次,而不是为每一行设置它,我通常在构造函数中或在填充行之前执行此操作。
//Clear any junk items that may have been there from the designer or previous view
((DataGridViewComboBoxColumn)MyDataTable.Columns["comboColumn"]).Items.Clear();
//Add you new values
((DataGridViewComboBoxColumn)MyDataTable.Columns["comboColumn"]).Items.AddRange(array);
然后设置新行的单元格的值
//Set value to be selected in new row
newrow["comboColumn"] = list[i].Customer_value;