2011-04-11 90 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

相關問題