【问题标题】:How do I insert a row into a DataGridView programmatically?如何以编程方式将行插入 DataGridView?
【发布时间】:2011-04-11 11:56:49
【问题描述】:

我正在尝试以编程方式将一行插入 DataGridView。列是:

Column1 组合框

Column2 文本框

这是我的代码。 Column1 使用 DataGridView 的内置 DataSource 绑定到表。

DataGridViewRow row = new DataGridViewRow();
DataGridViewComboBoxCell __action1 = new DataGridViewComboBoxCell();
DataGridViewTextBoxCell __site = new DataGridViewTextBoxCell();

__action.Value = 1; //1 is id.
__sitetype.Value = "google.com";

但这给了我“DataGridViewComboBoxCell 无效值”的错误。我该如何解决这个问题?

【问题讨论】:

  • 您的DataGridView 绑定到数据源了吗?如果是这样,则将行添加到数据源,而不是控件。
  • 只有combobox绑定到数据源,这样才可以填充list,文本列由代码设置。

标签: c# winforms datagridview


【解决方案1】:
DataGridViewRow dr = new DataGridViewRow();
DataGridViewCell dcDescription = new DataGridViewTextBoxCell();

dcDescription.Value = "some text";

dr.Cells.Add(dcDescription);
dataGridViewDoctorEventsAddServices.Rows.Add(dr);

【讨论】:

  • 还需要设置组合框的值,怎么做?
  • 是的,您应该在添加之前设置每个控件。
猜你喜欢
  • 2010-10-02
  • 2022-01-19
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2011-07-11
  • 1970-01-01
相关资源
最近更新 更多