【问题标题】:Datagridview comboboxes only in some rowsDatagridview 组合框仅在某些行中
【发布时间】:2012-12-08 11:16:18
【问题描述】:

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

是否可以以编程方式将该列中的某些组合框替换为其他内容? (例如标签上写着“n/a”)。我只想要某些行中的组合框。

【问题讨论】:

  • 虽然这种类型的功能是可能的(在某些行中放置标签而不是在其他行中),但性能影响很高。仅利用添加到显示值为N/A 的列表项的附加项的解决方案是否可以接受?

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


【解决方案1】:

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

在上面的示例中,我在第一列中分配了单个单元格属性。

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多