将ComboBox添加到DataGridView C#Winforms

时间:2014-04-10 12:31:59

标签: c# winforms datagridview combobox

我在将一个组合框下拉列表添加到DataGridView中的一行时遇到了一些麻烦,基本上它从未向组合框添加任何内容。它绘制它但它总是空白/ null。

DataGridViewRow dr = new DataGridViewRow();
                dr.CreateCells(dataGridView1,"","","",_NewAssetDetails[0], _NewAssetDetails[1], _NewAssetDetails[2],
                                       _NewAssetDetails[3], _NewAssetDetails[4], _NewAssetDetails[5],
                                       _NewAssetDetails[6], _NewAssetDetails[7], "", _NewAssetDetails[8],
                                       "New",_NewAssetDetails[9]);
                DataGridViewComboBoxCell cb = new DataGridViewComboBoxCell();
                cb.Items.Add(dr.Cells[13].Value);
                if (!cb.Items.Contains("Yes"))
                    cb.Items.Add("Yes");
                if (!cb.Items.Contains("No"))
                    cb.Items.Add("No");
                if (!cb.Items.Contains("Updated"))
                    cb.Items.Add("Updated");
                dr.Cells[13] = cb;
dataGridView1.Rows.Add(dr);

文本正确添加但组合框没有。 任何人都可以看到上面的问题吗?

1 个答案:

答案 0 :(得分:1)

我的代码在这里适用于我:

DataGridView1.Rows.Add(val[0], val[1], val[2], va[3], val[4]);
DataGridViewComboBoxCell cb= new DataGridViewComboBoxCell();
cb.MaxDropDownItems = 5;
cb.Items.Add(DataGridView1.Rows[DataGridView1.Rows.Count - 1].Cells[3].Value.ToString());
cb.Value = DataGridView1.Rows[DataGridView1.Rows.Count - 1].Cells[3].Value.ToString();
DataGridView1.Rows[DataGridView1.Rows.Count - 1].Cells[3] = cb;