Datagridview组合框仅在某些行中
问题描述:
在我的winforms应用程序中,我有一个DataGridView
,其中一行使用DataGridViewComboBoxColumn控件,以便它包含此列的组合框。Datagridview组合框仅在某些行中
是否有可能以其他方式编程替换该列中的某些组合框? (例如标签上写着“n/a”)。我只想要某些行中的组合框。
答
您应该使用DataGridViewComboBoxCell
属性而不是DataGridViewComboBoxColumn
属性。像下面这样:
for(int intCount=0;intCount<dgv.Rows.Count;intCount++)
{
if(intCount % 2 == 0) //Your own condition here
{
DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
//cmb.Value // assign values in the combobox
dgv[0,intCount] = cmb;
}
else
{
DataGridViewTextBoxCell txt = new DataGridViewTextBoxCell();
txt.Value = "n/a";
dgv[0,intCount] = txt;
}
}
在上面的例子中,我分配了第一列中的单个单元格属性。
尽管这种类型的功能是可能的(将标签放置在某些行中而不是其他标签中),但性能影响很大。是否可以接受一个解决方案,该解决方案只是将添加到显示值为“N/A”的列表项目中的附加项添加到“N/A”中? –