NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用

NewRowNeeded事件当 VirtualMode 属性为 true 时,将在用户定位到 DataGridView 底部的新行时发生,适合给新行建立一些默认数据和按规则应该产生的数据,但此时不推荐添加新数据到后台数据表中。因为用户操作有太多的变数,可以把产生的默认值直接赋值给一个临时初始数据对像即可,在用户触发UserAddedRow事件后,再赋值到后台数据也不迟
  1. Private pInitData As OuterExtendItem
  2. Private Sub xDataGridView_NewRowNeeded(sender As Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles xDataGridView.NewRowNeeded
  3. If (mHalt) Then
  4. Return
  5. End If
  6. pInitData = New OuterExtendItem()
  7. pInitData.mTitle = ""
  8. pInitData.mValue = ""
  9. End Sub





UserAddedRow事件是在编辑新行时触发,此时适合添加数据到后台数据表中
如例:

  1. Private Sub xDataGridView_UserAddedRow(sender As Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles xDataGridView.UserAddedRow
  2. If (mHalt) Then
  3. Return
  4. End If
  5. If (_ExtendList Is Nothing) Then
  6. Return
  7. End If
  8. If (pInitData IsNot Nothing) Then
  9. _ExtendList.addNotEvent(pInitData)
  10. pInitData = Nothing
  11. End If
  12. End Sub

//参考的数据显示代码:
  1. Private Sub xDataGridView_CellValueNeeded(sender As Object, e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles xDataGridView.CellValueNeeded
  2. If (mHalt) Then
  3. Return
  4. End If
  5. If (e.RowIndex < 0 OrElse e.RowIndex > _ExtendList.Count) Then
  6. Return
  7. End If
  8. If (e.RowIndex = _ExtendList.Count AndAlso pInitData IsNot Nothing) Then
  9. '显示新行的代码
  10. Select Case e.ColumnIndex
  11. Case xColumnTitle.Index
  12. e.Value = pInitData.mTitle
  13. Case xColumnValue.Index
  14. e.Value = pInitData.mValue
  15. End Select
  16. Else
  17. With _ExtendList
  18. Select Case e.ColumnIndex
  19. Case xColumnTitle.Index
  20. e.Value = .Item(e.RowIndex).mTitle
  21. Case xColumnValue.Index
  22. e.Value = .Item(e.RowIndex).mValue
  23. End Select
  24. End With
  25. End If
  26. End Sub


  1. Private Sub xDataGridView_CellValuePushed(sender As Object, e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles xDataGridView.CellValuePushed
  2. If (mHalt) Then
  3. Return
  4. End If
  5. If (e.RowIndex < 0 OrElse e.RowIndex >= _ExtendList.Count) Then
  6. Return
  7. End If
  8. With _ExtendList(e.RowIndex)
  9. Try
  10. Select Case e.ColumnIndex
  11. Case xColumnTitle.Index
  12. .mTitle = e.Value
  13. Case xColumnValue.Index
  14. .mValue = e.Value
  15. End Select
  16. .raiseValueChangedEvent(_ExtendList, EventArgs.Empty) //触发列表变化事件
  17. Catch exp As Exception
  18. MyHub.mBase.mDebugLog.writerError(Me, exp)
  19. Finally
  20. End Try
  21. End With
  22. End Sub
  1.   '列表变化事件,此处只要根据实际数据,设置行数即可
  2. Private Sub listChanged(sender As Object, ByVal e As DBListChangedEventArgs(Of OuterExtendItem))
  3. If (Me.InvokeRequired) Then
  4. Me.BeginInvoke(New EventHandler(Of DBListChangedEventArgs(Of OuterExtendItem))(AddressOf listChanged), New Object() {sender, e})
  5. Else
  6. xDataGridView.RowCount = _ExtendList.Count + 1 //行数+1行
  7. xDataGridView.Refresh()
  8. End If
  9. End Sub
RowsAdded事件,此事件是指在表格增加新行时触发,在初始化数据增加新行和后期编辑增加新行时都会触发此事件,而UserAddedRow事件一般是指在编辑阶段由用户操作触发的增加新行事件,二者有所区别,UserAddedRow事件使用率会比较高一点,RowsAdded事件适用于产生一些根据绑定数据需要额外显示的信息时。

posted on 2015-11-12 11:48  蒋雷军  阅读(1761)  评论(0编辑  收藏  举报

导航