标签:
NewRowNeeded事件当 VirtualMode 属性为 true 时,将在用户定位到 DataGridView 底部的新行时发生,适合给新行建立一些默认数据和按规则应该产生的数据,但此时不推荐添加新数据到后台数据表中。因为用户操作有太多的变数,可以把产生的默认值直接赋值给一个临时初始数据对像即可,在用户触发UserAddedRow事件后,再赋值到后台数据也不迟
Private pInitData As OuterExtendItem
Private Sub xDataGridView_NewRowNeeded(sender As Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles xDataGridView.NewRowNeeded
If (mHalt) Then
Return
End If
pInitData = New OuterExtendItem()
pInitData.mTitle = ""
pInitData.mValue = ""
End Sub
Private Sub xDataGridView_UserAddedRow(sender As Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles xDataGridView.UserAddedRow
If (mHalt) Then
Return
End If
If (_ExtendList Is Nothing) Then
Return
End If
If (pInitData IsNot Nothing) Then
_ExtendList.addNotEvent(pInitData)
pInitData = Nothing
End If
End Sub
Private Sub xDataGridView_CellValueNeeded(sender As Object, e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles xDataGridView.CellValueNeeded
If (mHalt) Then
Return
End If
If (e.RowIndex < 0 OrElse e.RowIndex > _ExtendList.Count) Then
Return
End If
If (e.RowIndex = _ExtendList.Count AndAlso pInitData IsNot Nothing) Then
- ‘显示新行的代码
Select Case e.ColumnIndex
Case xColumnTitle.Index
e.Value = pInitData.mTitle
Case xColumnValue.Index
e.Value = pInitData.mValue
End Select
Else
With _ExtendList
Select Case e.ColumnIndex
Case xColumnTitle.Index
e.Value = .Item(e.RowIndex).mTitle
Case xColumnValue.Index
e.Value = .Item(e.RowIndex).mValue
End Select
End With
End If
End Sub
Private Sub xDataGridView_CellValuePushed(sender As Object, e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles xDataGridView.CellValuePushed
If (mHalt) Then
Return
End If
If (e.RowIndex < 0 OrElse e.RowIndex >= _ExtendList.Count) Then
Return
End If
With _ExtendList(e.RowIndex)
Try
Select Case e.ColumnIndex
Case xColumnTitle.Index
.mTitle = e.Value
Case xColumnValue.Index
.mValue = e.Value
End Select
.raiseValueChangedEvent(_ExtendList, EventArgs.Empty) //触发列表变化事件
Catch exp As Exception
MyHub.mBase.mDebugLog.writerError(Me, exp)
Finally
End Try
End With
End Sub
‘列表变化事件,此处只要根据实际数据,设置行数即可
Private Sub listChanged(sender As Object, ByVal e As DBListChangedEventArgs(Of OuterExtendItem))
If (Me.InvokeRequired) Then
Me.BeginInvoke(New EventHandler(Of DBListChangedEventArgs(Of OuterExtendItem))(AddressOf listChanged), New Object() {sender, e})
Else
xDataGridView.RowCount = _ExtendList.Count + 1 //行数+1行
xDataGridView.Refresh()
End If
End Sub
NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用
标签:
原文地址:http://www.cnblogs.com/wene/p/4958543.html