当DataGridView在VirtualMode模式下NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用
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 OrElse mGaugeConfig.mOPCItems Is Nothing) Then
Return
End If
If (mGaugeConfig.mOPCItems.Count < 15) Then
_InitData = New OPCItemConfig(mGaugeConfig.mGID)
With _InitData
.fMin = Convert.ToDouble(Int16.MinValue)
.fMax = Convert.ToDouble(Int16.MaxValue)
.mRemark = ""
.mUnitAdjust = 1.0
.mDataField = _fieldList(0).mValue
End With
Else
If (xDataGridView.RowCount > mGaugeConfig.mOPCItems.Count + 1) Then
xDataGridView.CancelEdit()
End If
MsgBox("OPC项最多只允许配置15个!", MsgBoxStyle.OkOnly, MyHub.mBase.appTitle)
End If
checkControl()
End Sub
官方说明:https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.newrowneeded(VS.80).aspx
UserAddedRow事件是在编辑新行时触发,此时适合添加数据到后台数据表中
如例:
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
ElseIf (e.RowIndex < _ExtendList.Count) Then
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
RowsAdded事件,此事件是指在表格增加新行时触发,在初始化数据增加新行和后期编辑增加新行时都会触发此事件,而UserAddedRow事件一般是指在编辑阶段由用户操作触发的增加新行事件,二者有所区别,UserAddedRow事件使用率会比较高一点,RowsAdded事件适用于产生一些根据绑定数据需要额外显示的信息时。