2011-04-11 71 views
1

我想以编程方式将一行插入到DataGridView中。列是:如何以编程方式将行插入到DataGridView中?

列1组合框

列2文本

这是我的代码。 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无效值”的错误。我该如何解决?

+0

你的'DataGridView' **绑定**到数据源吗?如果是这样,您将行添加到数据源,而不是控件。 – 2011-04-11 12:20:30

+0

只有组合框绑定到数据源,以便它可以填充列表,文本列是从代码设置的。 – 2011-04-11 12:25:41

回答

2
DataGridViewRow dr = new DataGridViewRow(); 
DataGridViewCell dcDescription = new DataGridViewTextBoxCell(); 

dcDescription.Value = "some text"; 

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

还需要设置组合框的值,该怎么做? – 2011-04-11 12:19:13

+1

是的,你应该在添加它之前设置每个控件。 – TOUDIdel 2011-04-12 07:32:29

相关问题