2013-06-14 2 views
0

DataTable에 바인딩 된 DataGridView이 데이터베이스에서 채워집니다.DataGridView DataError가 첫 번째 행에서 throw되지 않았습니다.

Dim ComboBoxData As ArrayList = New ArrayList(New String() {"", 
                  "Default", 
                  "User-set"}) 
Dim ComboBoxBS As BindingSource = New BindingSource 
ComboBoxBS.DataSource = ComboBoxData 
SomeDataGridViewComboBoxColumn.DataSource = ComboBoxBS 

나는 DataTable의 필드가 ComboBoxData에 정의 된 이외의 값을 가질 수 있도록하려면이 그리드, 난과 같이 ArrayList의 결합 데이터 DataGridViewComboBoxColumn s의 몇 가지있다. 이것은 내가 DataError 이벤트를 처리하고 ComboBox에 누락 된 값을 추가하여 수행 한 :

Private Sub grid_DataError(ByVal sender As System.Object, 
          ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs 
          ) Handles grid.DataError 
    If TypeOf grid.Columns(e.ColumnIndex) Is DataGridViewComboBoxColumn Then 
     Dim value = grid.Rows(e.RowIndex).Cell(e.ColumnIndex).Value 
     Dim cboCol As DataGridViewComboBoxColumn = CType(grdDataGrid2.Columns(e.ColumnIndex), DataGridViewComboBoxColumn) 
     Dim bs As BindingSource = CType(cboCol.DataSource, BindingSource) 
     If Not bs.Contains(value) Then 
      bs.Add(value) 
      bs.Position = bs.IndexOf(value) 
     End If 
     e.Cancel = False 
     Return 
    End If 
End Sub 

이 첫 번째 제외한 모든 행에 대해 작동합니다. 디버깅을 통해 DataError 이벤트가 실제로 첫 번째 행에 대해 실행되지 않음을 확인했습니다. 그러나 "기준 표현식의 데이터 유형 불일치"라는 행 헤더에 빨간색 느낌표가 있습니다.

왜 첫 번째 행이 나머지로 동작하지 않습니까? 이 문제에 대한 더 나은 접근법을 제안하십시오.

+0

확실치 않지만 모든 테이블의 첫 번째 행은 대부분 헤더 행입니다. 그게 뭔가를 일으킬 수는 없습니까? – Terry

+0

나는 체크했다. 첫 번째 행 (인덱스 0)은 헤더가 아닌 데이터가있는 첫 번째 행입니다. – joharei

답변

0

DataGridViewComboBoxColumn 대신 DataGridViewComboBoxCell을 참조 해보십시오. 필자 자신의 상황에서는 데이터 테이블에 결합 된 comboboxcolumn을 사용하여 셀을 참조했을 때 첫 번째 행을 적절한 새 값으로 업데이트 할 수있었습니다. 아래 코드의 마지막 줄에서했던 것처럼 값을 재 할당해야 할 수도 있습니다.

Dim comboCell As DataGridViewComboBoxCell = DirectCast(targetGrid.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewComboBoxCell) 
Dim comboData As DataTable = DirectCast(comboCell.DataSource, DataTable) 
Dim oRow As DataRowView = comboData.DefaultView.AddNew() 
oRow(oComboCol.DataPropertyName) = targetValue 
oRow.EndEdit() 
comboData.AcceptChanges() 
comboCell.Value = targetValue 
관련 문제