简体   繁体   English

将ComboBox添加到DataGridView C#Winforms

[英]Add ComboBox to DataGridView C# Winforms

I am having some trouble adding a combobox drop down list to a row in a DataGridView, basically it never adds anything to the combobox. 我在将组合框下拉列表添加到DataGridView中的一行时遇到了一些麻烦,基本上,它从不向组合框添加任何内容。 It draws it but it is always blank/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);

The text gets added correctly but the combobox does not. 文本已正确添加,但组合框未正确添加。 Can anyone see a problem above? 有人可以看到上面的问题吗?

I have code that works for me here it is: 我这里有适合我的代码:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM