一直在这个问题上走入误区
误区1:总是试图像ASP.Net下那样通过FindControl获取各行的具体控件
误区2:总是试图避免产生DataGridview的DataError事件
看了很多文章后,终于接受了Combobox列中各单元格中控件间类似于克隆的关系的现实.
接受了这样的现实后,如果还对Combobox列有更个性化的要求,又不愿意做太多的重写工作,性能上的牺牲就是避免不了的.
可以这样弄:
第一.在适当的地方添加对Combobox下拉列表的处理
Private
Sub
DataGridView1_CellBeginEdit(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.DataGridViewCellCancelEventArgs)
Handles
DataGridView1.CellBeginEdit
If e.ColumnIndex = 1 Then
Dim TempDataGridViewComboBoxCell As DataGridViewComboBoxCell = CType (DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
TempDataGridViewComboBoxCell.Dispose()
TempDataGridViewComboBoxCell.Items.Add( Rnd )
TempDataGridViewComboBoxCell.Items.Add( Rnd )
TempDataGridViewComboBoxCell.Items.Add( Rnd )
TempDataGridViewComboBoxCell.Items.Add( Rnd )
End If
End Sub
If e.ColumnIndex = 1 Then
Dim TempDataGridViewComboBoxCell As DataGridViewComboBoxCell = CType (DataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
TempDataGridViewComboBoxCell.Dispose()
TempDataGridViewComboBoxCell.Items.Add( Rnd )
TempDataGridViewComboBoxCell.Items.Add( Rnd )
TempDataGridViewComboBoxCell.Items.Add( Rnd )
TempDataGridViewComboBoxCell.Items.Add( Rnd )
End If
End Sub
第二.就是添加一个DataError事件,处理可能会产生的错误,处理错误的办法就是什么也不做.
Private Sub DataGridView1_DataError( ByVal sender As Object , ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
' 这里空着就可以了
End Sub