【问题标题】:Add ComboBox to DataGridView C# Winforms将 ComboBox 添加到 DataGridView C# Winforms
【发布时间】:2014-04-10 12:31:59
【问题描述】:

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

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

文本被正确添加,但组合框没有。 谁能看到上面的问题?

【问题讨论】:

  • 你可以试试先添加行然后dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[13] = cb;
  • 不幸的是组合框中仍然没有值。问题是,如果我单步执行代码,我可以看到 row.Cells 中的单元格类型发生了变化,我什至可以看到组合框中的所有值,但它只是没有显示出来
  • 我在 Form1_Activated 中执行该操作,并检查是否有来自第二个表单的传入数据。

标签: c# winforms datagridview combobox


【解决方案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;

【讨论】:

  • 嗯,我已经添加了dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[13].Value = "New";,这似乎已经解决了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-28
相关资源
最近更新 更多