2012-02-01 14 views
0

DataSet(DataSetSecurity)のDataTable(DataTableSecurity)にバインドされているVB.net(Visual Studio 2010)のDataGridView(DataGridViewSecurity)があります。 DataTableの整数フィールド(nSecLevelと呼ばれる)に基づいて設定した非バインド列(nSecurityComboBox)を追加しました。コンボボックスを設定すると、コンボボックスに何も表示されませんが、コンボボックスを選択すると、アイテムコレクションの5つの値が表示されます。ここでDatagridcomboboxの値が表示されない

は、私がデータテーブルにレコードを追加し、その後、コンボボックスを設定するために使用しているコードです:

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

答えて

0

最後に、問題を解決するために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 
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) 

これが役に立ちます。

関連する問題