1. 在DataGrid程序中批量修改数据

    要达到的效果是使DataGrid里的数据修改可以像Excel一样方便易用。
     具体步骤如下:
      
      (1)DataGrid绑定到DataView
      (2)使DataGrid处于可以编辑状态下,DataView处于允许编辑,新增状态
      (3)编辑数据
      (4)编辑完毕后,移开焦点,这步很关键,如果不移开焦点,当前修改的哪个单元格   就被认为数据没有修改,具体的操作如下:
        

Winform下DataGrid使用技巧总结 Dim a As Integer
Winform下DataGrid使用技巧总结                        a 
= Me.DataGrid3.CurrentRowIndex + 1
Winform下DataGrid使用技巧总结                        
Me.DataGrid3.CurrentCell = New DataGridCell(a, 10)

其中的10 要根据读进来的数据进行改变
      (5)检查DataGrid的数据是否被修改,如果有就执行第 (6)步,否则到此结束。
           检查的操作如下:
          

Winform下DataGrid使用技巧总结 If objDs.HasChanges(DataRowState.Modified) = False Then
Winform下DataGrid使用技巧总结            
MsgBox("沒有數據被修改", MsgBoxStyle.OKOnly, "數據保存")
Winform下DataGrid使用技巧总结
Exit Sub
Winform下DataGrid使用技巧总结        
End If

      (6)利用SqlDataAdapter的Update来更新数据
              具体操作如下
         
End Function


           (7)最后是一些修改后的重新绑定操作。具体要看是新增还是修改(一次只能执行一种操作),如果是新增就要求重新读一下数据,如果是修改,那就在AcceptChanges后,重新绑定DataView就可以了。

            在WebForm下要用datagrid做批量修改,更新的原理是一样的,不过Datagrid里要使用模板列。

            2.获取DataGrid里选中的行。
             具体步骤:
              (1)DataGrid绑定到DataView
              (2)选中行
              (3)获取行
               

Winform下DataGrid使用技巧总结Dim dm1 As BindingManagerBase
Winform下DataGrid使用技巧总结        dm1 
= Me.BindingContext(objDsTotal, "DataTabelName ")
Winform下DataGrid使用技巧总结        
Dim i, count As Integer
Winform下DataGrid使用技巧总结
Winform下DataGrid使用技巧总结
Winform下DataGrid使用技巧总结        
For i = 0 To dm1.Count - 1
Winform下DataGrid使用技巧总结            
If Me.DataGrid1.dgdGridShow.IsSelected(i) Then
Winform下DataGrid使用技巧总结
Winform下DataGrid使用技巧总结          
'do what you want to do with the select row
Winform下DataGrid使用技巧总结

Winform下DataGrid使用技巧总结                count 
= count + 1
Winform下DataGrid使用技巧总结            
End If
Winform下DataGrid使用技巧总结        
Next

              (4)最后可以根据count的值来判断是否做其他(如更新)操作,更新的原理和上面说的一样。

相关文章:

  • 2021-04-02
  • 2021-11-13
  • 2021-05-31
  • 2021-11-28
  • 2021-09-28
  • 2021-08-05
  • 2022-01-18
猜你喜欢
  • 2021-12-22
  • 2022-01-06
  • 2022-01-07
  • 2021-07-11
  • 2021-10-10
  • 2021-06-01
相关资源
相似解决方案