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 に答える