DataGridView获取当前绑定项的实例对像DataGridView1.CurrentRow.DataBoundItem
一般写法:
If (Me.xDataGridView.CurrentRow Is Nothing) Then
Return Nothing
End If
Dim pView As ProductEntity = CType(Me.xDataGridView.CurrentRow.DataBoundItem, ProductEntity)
Return pView
当表格中有数据时,不会有什么问题,但当没有数据时,就会出现如下异常
因为xDataGridView.CurrentRow在没有数据时,也不会是nothing,xDataGridView.CurrentRow.DataBoundItem也不会是nothing,而是非常奇怪的WhereSelectEnumerableIte*这样一个对像
所以完善的写法应该是如下
If (Me.xDataGridView1.CurrentRow Is Nothing) Then
Return Nothing
End If
If (TypeOf Me.xDataGridView1.CurrentRow.DataBoundItem Is Base_Alarm) Then
Return CType(Me.xDataGridView1.CurrentRow.DataBoundItem, Base_Alarm)
Else
Return Nothing
End If