0

我在 VB.net(Visual Studio 2010)中有一个 DataGridView(称为 DataGridViewSecurity),它绑定到 DataSet(称为 DataSetSecurity)中的一个 DataTable(称为 DataTableSecurity)。我添加了一个基于 DataTable 中的整数字段(称为 nSecLevel)设置的非绑定列(称为 nSecurityComboBox)。设置组合框后,它不会在组合框中显示任何内容,但是当您选择组合框时,它的项目集合中的 5 个值会显示。

这是我用来向 DataTable 添加记录然后设置组合框的代码:

Sub Foo()
.
.
.
    DataSetSecurity.Tables(0).Rows.Add(New Object() {sName, sID, sSec})
    ComboCell_Select(nRow, 3, DataGridViewSecurity, sSecRecs.nSecLevel)
    MessageBox.Show("Value for the combo set at " + DataGridViewSecurity.Rows(nRow).Cells(3).Value.ToString)
.
.
.
End Sub

Private Sub ComboCell_Select(ByVal dgvRow As Integer, _
                             ByVal dgvCol As Integer, _
                             ByRef DGV As DataGridView,
                             ByRef nComboBoxRow As Int16)

    Try
        Dim CBox As DataGridViewComboBoxCell = CType(DGV.Rows(dgvRow).Cells(dgvCol), DataGridViewComboBoxCell)
        Dim CCol As DataGridViewComboBoxColumn = CType(DGV.Columns(dgvCol), DataGridViewComboBoxColumn)

        CBox.Value = CCol.Items(nComboBoxRow)
        DGV.UpdateCellValue(dgvCol, dgvRow)

        'MessageBox.Show("New value in the combo box = " + CBox.Value.ToString)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Foo 中的 messagebox.show 显示了组合框的正确值,但没有显示任何内容。有人看到我做错了什么吗?

谢谢。

-NCGrimbo

4

2 回答 2

1

最后,我找到了一些我转换为 VB.net 的 C# 代码来解决这个问题。这是代码:

Private Sub DataGridViewSecurity_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridViewSecurity.EditingControlShowing
    Dim cellComboBox As ComboBox = TryCast(e.Control, ComboBox)
    If cellComboBox IsNot Nothing Then
        ' make sure the handler doen't get registered twice
        RemoveHandler cellComboBox.SelectionChangeCommitted, AddressOf Me.CellComboBoxOnSelectionChangeCommitted
        AddHandler cellComboBox.SelectionChangeCommitted, AddressOf Me.CellComboBoxOnSelectionChangeCommitted
    End If
End Sub

Private Sub CellComboBoxOnSelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)
    Dim comboBox As DataGridViewComboBoxEditingControl = TryCast(sender, DataGridViewComboBoxEditingControl)
    If sender Is Nothing Then
        Return
    End If
    If comboBox.SelectedItem Is Nothing Then
        Return
    End If
    If Me.DataGridViewSecurity.CurrentCell.Value = comboBox.SelectedItem Then
        Return
    End If

    Me.DataGridViewSecurity.CurrentCell.Value = comboBox.SelectedItem

End Sub
于 2012-02-10T20:16:19.783 回答
0

如果我正确理解了这个问题,那么所有值都在组合框中,只是默认情况下没有正确选择?我想我几天前刚遇到这个问题,这就是我现在遇到的问题。

'Create the combobox column
Dim comboBox As New DataGridViewComboBoxColumn()

'Add some stuff to the combobox
comboBox.Items.Add("FirstItem")
comboBox.Items.Add("SecondItem")

'Select the first item
comboBox.DefaultCellStyle.NullValue = comboBox.Items(0) 

'Now add the whole combobox to the DataGridView
dgvItems.Columns.Add(comboBox)

希望这可以帮助!

于 2012-02-03T14:04:34.867 回答