【问题标题】:Set SelectedItem in DataGridViewComboBoxCell在 DataGridViewComboBoxCell 中设置 SelectedItem
【发布时间】:2015-01-28 20:40:53
【问题描述】:

我将DataGridViewComboBoxCell 添加到datagridview 列,并希望将第一项设置为selectedItem / selectedIndex,但如果只分配datasource,则有一些示例可以实现此目的,但我不想使用数据源

这是我的代码

 Dim col As New DataGridViewComboBoxCell

 For Each r As DataRow In myDataTable.Rows
     col.Items.Add(r("Months"))
 Next

 Dim row As New DataGridViewRow
 row.CreateCells(dgv)
 row.Cells(0) = col

 dgv.Rows.Add(row)

谢谢

【问题讨论】:

  • 我不确定您的问题,或者我想知道问题出在哪里。代码有效吗?
  • 是的,代码在工作,我可以看到组合框中所有添加的项目,但第一项没有被选中,问题是如何将组合框的第一项设置为选中项?

标签: .net vb.net datagridview


【解决方案1】:

在处理更改后的 DataGridView 列时,您希望使用 Value 属性。在row.Cells(0) = col之前某处添加这行代码:

col.Value = col.Items(0)

【讨论】:

  • 不工作,显示'Items' is not a member of 'System.Windows.Forms.DataGridViewCell'
  • @Ahmed 哎呀。忘记了,您必须将单元格转换回 DataGridViewComboBoxCell,或者对Dim col 执行相同的操作。请参阅我编辑的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
相关资源
最近更新 更多