【问题标题】:DataGridViewComboBoxCell won't dropDataGridViewComboBoxCell 不会掉落
【发布时间】:2017-09-20 12:22:44
【问题描述】:

我在 Visual Studio 2010 中使用 vb.NET。我找到了一个如何将 ComboBox 添加到 DataGridView 单元格的示例,并将其添加到我的代码中。当我运行代码并添加一个新行时,ComboBox 是可见的,但它没有显示任何值,也不会下拉。

我是否遗漏了代码中的某些内容? DataGridView 是否需要设置某些属性?

dgvFiles.Rows.Add({"Cell1","Cell2"})
Dim gridComboBox As New DataGridViewComboBoxCell
gridComboBox.Items.Add("A") 'Populate the Combobox
gridComboBox.Items.Add("B") 'Populate the Combobox
gridComboBox.Items.Add("C") 'Populate the Combobox
dgvFiles(2, dgvFiles.Rows.Count - 1) = gridComboBox

编辑:

我在设计时设置了四列,这不是问题。问题原来是我已将 DataGridView 设置为“EditProgrammatically”。最初我已将其更改为阻止用户编辑文本单元格,但显然,它阻止了组合框掉落。

我感谢所有给出的答案。很抱歉我忘了提及我在设计时设置了四列,而这个问题是由于我没有意识到 EditProgrammatically 设置有这种效果而引起的。

【问题讨论】:

  • 我相信你必须添加一个datagridviewcomboboxcolumn并将comboboxcell添加到该列。喜欢这里:stackoverflow.com/questions/11657345/…
  • 如果你的值是静态的,你也可以在设计模式下完成所有设置,而不是使用代码。
  • 您不必像雅各布所说的那样制作整个专栏。网格中可能只有 1 个组合框。

标签: vb.net datagridview datagridviewcomboboxcell


【解决方案1】:

您的代码几乎没问题。一切都下降了。您可以在列表中显示默认值。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dgvfiles.Columns.Add("Column1", "Column 1")
        dgvfiles.Columns.Add("Column2", "Column 2")
        dgvfiles.Columns.Add("Column3", "Column 3")
        dgvfiles.Columns.Add("Column4", "Column 4")
        dgvfiles.Rows.Add({"Cell1", "Cell2"})
        Dim gridComboBox As New DataGridViewComboBoxCell
        gridComboBox.Items.Add("A") 'Populate the Combobox
        gridComboBox.Items.Add("B") 'Populate the Combobox
        gridComboBox.Items.Add("C") 'Populate the Combobox
        gridComboBox.Value = gridComboBox.Items(0)
        dgvfiles(2, dgvfiles.Rows.Count - 2) = gridComboBox
    End Sub

    Private Sub dgvfiles_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvfiles.CellBeginEdit
        If e.ColumnIndex = 2 Then
            'Do something
        Else
            e.Cancel = True
        End If
    End Sub

【讨论】:

  • 但是DataGridView在设计模式下设置了四列。
  • 添加 gridComboBox.Value = gridComboBox.Items(0) 使它显示一个值,但它仍然不会让我下拉。我将使用空白的 DataGridView 制作一个测试项目,看看它是否适用于该项目,以便我可以查看我是否在设计时更改了一个搞砸的设置。
  • 好吧,显然,它不起作用,因为我将它设置为“以编程方式编辑”。但是将其更改为其他任何内容会使其他单元格可编辑,这是我不想要的。我想,我得想办法让其他单元格不可编辑。
  • 我没想到你会读懂我的想法,我只是忘了说我在设计时设置了多少列。当你说“你只添加 2 列”时,我意识到我没有提到它,所以我回答说我已经设置了 4 列。关于需要编辑组合框而不是其他单元格的部分似乎并不相关,因为我不知道 EditProgrammatically 会阻止组合框工作,并且无论您声明将网格更改为以编程方式编辑不会影响它,这正是我的程序中发生的事情。
  • @jcvamp 将此添加到 beginedit 并将您的网格设置为 editonkeystoke
猜你喜欢
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2017-01-19
相关资源
最近更新 更多