【问题标题】:Adding DatagridviewComboboxCell to new row将 DatagridviewComboboxCell 添加到新行
【发布时间】:2012-05-02 17:56:51
【问题描述】:

我在按钮单击事件上添加一行并在行单元格中添加组合框,但我无法绑定该单元格组合框中的值。谁能建议我做错了什么??

DataGridViewRow dgr = new DataGridViewRow();            

DataGridViewComboBoxCell cbCol = new DataGridViewComboBoxCell();
cbCol.Items.Add("Test");
cbCol.Items.Add("Test");
cbCol.Items.Add("Test");
cbCol.Items.Add("Test");       

dgr.Cells.Add(cbCol);

dataGridView1.Rows.Add(dgr);

上面的代码写在一个按钮点击事件中。

【问题讨论】:

    标签: winforms c#-4.0 datagridview datagridviewcomboboxcell


    【解决方案1】:

    您尝试做的事情不应该是必要的。

    您的网格应该有一个 DataGridViewComboBoxColumn 类型的列,其中有一个 DataSource 集,为该列中的所有单元格提供可用的值。

    当您向网格添加新行时,您将自动在组合框列中获得一个包含您需要的值的单元格。如果您想在每一行中有不同的值,那么您可以使用网格的EditingControlShowing 事件来更改组合框数据源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      相关资源
      最近更新 更多