-1

似乎它是如此简单,但由于某种原因,它持续了 4 个小时没有结果。我有两列和几行已经填充的简单 Gridview,现在我想将 ComboBoxCell 添加到下一行。

我会展示我的逻辑,也许你会告诉我我的错误:

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

现在我正在插入我的 ComboBox 三行工作正常:

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);

有任何想法吗?谢谢

4

2 回答 2

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);
于 2013-01-26T01:21:42.050 回答
0
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.datasource = stringlist;

SampleGridView.Columns.Add(combo);

这不在我的脑海中,但应该可以。

于 2013-01-22T23:12:27.760 回答