我想在我的DataGridView中分别设置每个DataGridViewComboBoxCell的项目(因为每个组合框必须有不同的项目)。我用这个代码来设置项目:设置DataGridViewComboBoxCell项目
foreach (DataGridViewRow row in grid.Rows)
{
((DataGridViewComboBoxCell)row.Cells[1]).Items.Clear();
foreach (Product prod in _ProductList)
{
((DataGridViewComboBoxCell)row.Cells[1]).Items.Add(prod.Name);
}
}
调试我看到的DataGridViewComboBoxCell的项目设置正确,但是当我看到网格,组合技是空的。
进行不同的测试我意识到,如果我在表单加载后设置项目(例如在点击事件中),项目显示正常。
我应该如何在表单加载时加载项目?
DataGridViewCombox有一个DataSource连接到它..我想知道是否将该特定项目的数据源设置为string.Empty将解决您的问题..听起来像一个DataBinding问题看这个链接可能选择备用DataSource然后设置您试图通过foreach循环设置的项目,你有http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcell.aspx – MethodMan 2012-02-13 15:54:20
这是另一个有用的链接,将解释它甚至更多.. http://homepage.ntlworld.com/herring1/datagrid.html – MethodMan 2012-02-13 15:59:07
或者我尝试使用DataSource而不是在一段时间内设置项目。但问题依然如故。 如果我将DataGridViewComboBoxColumn设置为DataGridViewComboBoxCell,则网格中的项目已正确填充,但此解决方案对我来说不可行,因为每个单元格都有不同的项目。 – joaocarlospf 2012-02-13 17:31:58