Datagridview仅在某些行中组合

时间:2012-12-08 11:16:18

标签: c# .net winforms data-binding datagridview

在我的winforms应用程序中,我有一个DataGridView,其中一行使用DataGridViewComboBoxColumn控件,因此它包含此列的组合框。

是否有可能以其他方式以编程方式替换该列中的某些组合框? (例如标签上写着“不适用”)。我只想在某些行中使用组合框。

1 个答案:

答案 0 :(得分:2)

您应该使用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;
   }
}

在上面的例子中,我在第一列中分配了Individual单元属性。