Значение Datagridcombobox не отображается

У меня есть DataGridView (называемый DataGridViewSecurity) в VB.net (Visual Studio 2010), который привязан к DataTable (называемый DataTableSecurity) в наборе данных (называемый DataSetSecurity). Я добавил несвязанный столбец (названный nSecurityComboBox), который я установил на основе целочисленного поля (названного nSecLevel) в DataTable. После настройки поля со списком он ничего не отображает в поле со списком, но когда вы выбираете поле со списком, отображаются 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

messagebox.show в Foo показывает правильное значение поля со списком, но ничего не отображается. Кто-нибудь видит, что я делаю неправильно?

Спасибо.

-NCGrimbo


person NCGrimbo    schedule 01.02.2012    source источник


Ответы (2)


Если я правильно понимаю вопрос, все значения в поле со списком просто не выбираются по умолчанию должным образом? Я думаю, что у меня была эта проблема несколько дней назад, вот что у меня сейчас.

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

Надеюсь это поможет!

person Boeckm    schedule 03.02.2012

В конце концов я нашел некоторый код C#, который я преобразовал в VB.net, чтобы исправить проблему. Вот код:

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
person NCGrimbo    schedule 10.02.2012