将项目添加到DataGridView中的组合框
问题描述:
我在winforms应用程序中有一个DataGridView.除了来自db表的4列之外,我还需要显示一个在datagridview中具有组合框的附加列[可能正在使用DataGridViewComboColumn?].2.然后,我想为每一行的每个组合框添加不同的项目集.
I've a DataGridView in a winforms app. Apart from the 4 columns coming from the db table,I need to show an additional column having a combobox in the datagridview[may be using DataGridViewComboColumn?]. 2.And then I want to add different set of items to each combobox for every row.
我该怎么办?
谢谢.
答
您可以尝试通过网格的 DataBindingComplete 添加它们
You may try to add them via DataBindingComplete of the grid
这些东西上的东西
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0] is DataGridViewComboBoxCell && row.Index == 1)
(row.Cells[0] as DataGridViewComboBoxCell).Items.Add("A");
else
(row.Cells[0] as DataGridViewComboBoxCell).Items.Add("B");
}
}
希望这会有所帮助编辑
(row.Cells[0] as DataGridViewComboBoxCell).Value = (row.Cells[0] as DataGridViewComboBoxCell).Items[0];
选中该单元格后,第一个值将显示为选中状态
When that cell is selected then the first value would be shown selected