2012-02-01 14 views
0

DataSet (DataSetSecurity)의 DataTable (DataTableSecurity)에 바인딩 된 VB.net (Visual Studio 2010)의 DataGridView (DataGridViewSecurity)가 있습니다. DataTable의 정수 필드 (nSecLevel)를 기반으로 설정 한 비 바운드 열 (nSecurityComboBox)을 추가했습니다. 콤보 상자를 설정 한 후에는 콤보 상자에 아무 것도 표시하지 않지만 콤보 상자를 선택하면 항목 모음에 5 개의 값이 표시됩니다.Datagridcombobox 값이 표시되지 않습니다.

여기에 내가 콤보 상자 설정하기 위해 다음의 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이 콤보에 대한 올바른 값을 보여줍니다,하지만 아무것도 표시되지 않습니다 . 누구나 내가 뭘 잘못하고 있는지 알아?

감사합니다.

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

희망이 있습니다.

관련 문제