2013-01-22 79 views
-1

似乎很簡單,但由於某種原因,最後4小時沒有任何結果。 我有簡單的Gridview,有兩列和幾行已經填滿,現在我想添加ComboBoxCell到下一行。將DataGridViewComboBoxCell添加到GridView c#

我會告訴我的邏輯,也許你會告訴我,我的錯誤:

SampleGridView.Rows.Add("test1", "test1"); 
SampleGridView.Rows.Add("test2", "test2"); 
SampleGridView.Rows.Add("test3", "test3"); 

工作正常三行現在我將我的組合框:

DataGridViewRow RowSample = new DataGridViewRow(); 
DataGridViewComboBoxCell CellSample = new DataGridViewComboBoxCell(); 
CellSample.DataSource = StringList; // list of the items that I want to insert in ComboBox 
RowSample.Cells.Add(CellSample); 
SampleGridView.Rows.Add(RowSample); 

任何想法?謝謝

+0

對不起的人,我認爲這是ASP.NET。它是WinForms的權利? –

+0

不是問題.. – inside

回答

1

想通了:)我試圖插入ComboBox到行的第二個單元格,它沒有工作。我試圖做這樣的:

SampleGridView.Rows.Add("CellText", RowSample); 

但它實際上必須這樣

DataGridViewRow RowSample = new DataGridViewRow(); 
DataGridViewComboBoxCell CellSample = new DataGridViewComboBoxCell(); 
CellSample.DataSource = StringList; // list of the string items that I want to insert in ComboBox 
CellSample.Value = StringList[0]; // default value for the ComboBox 
DataGridViewCell cell = new DataGridViewTextBoxCell(); 
cell.Value = "CellText"; // creating the text cell 
RowSample.Cells.Add(cell); 
RowSample.Cells.Add(CellSample); 
SampleGridView.Rows.Add(RowSample); 
0
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn(); 
combo.datasource = stringlist; 

SampleGridView.Columns.Add(combo); 

這是關閉我的頭頂,但應該工作。

+0

這會將組合框添加到列中,但不會添加到特定行 – inside

+0

您是否想要爲每行都有不同的數據源? – Derek

+0

基本上只有一行必須是ComboBox,其他都只是文本行 – inside