2011年12月5日 星期一

datagridview 拖曳至另一datagridview

參考引用
--
  1. '將DataGridView1及DataGridView2的AllowDrop屬性設為true
  2.  
  3. Dim cn As New SqlConnection("server=192.168.7.23;database=northwind;user id=sa;password=sa")
  4. Dim cmd As New SqlCommand("select customerid,companyname,city from customers", cn)
  5. Dim da As New SqlDataAdapter
  6. Dim ds As New DataSet
  7.  
  8. Dim index As Integer
  9.  
  10. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11. da.SelectCommand = cmd
  12. da.Fill(ds, "customers")
  13.  
  14. cmd.CommandText = "select customerid,companyname,'' as temp,city from customers"
  15. da.FillSchema(ds, SchemaType.Source, "customers2")
  16.  
  17. DataGridView1.DataSource = ds.Tables("customers")
  18. DataGridView2.DataSource = ds.Tables("customers2")
  19. End Sub
  20.  
  21. 'DataGridView1拖曳到DataGridView2
  22. Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
  23. If e.Button = Windows.Forms.MouseButtons.Left Then
  24. index = e.RowIndex
  25. DataGridView1.DoDragDrop(ds.Tables("customers").Rows(e.RowIndex), DragDropEffects.Move)
  26. End If
  27. End Sub
  28.  
  29. Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop
  30. Dim dr As DataRow = e.Data.GetData(GetType(DataRow))
  31.  
  32. Dim new_dr As DataRow = ds.Tables("customers2").NewRow
  33.  
  34. Dim j As Integer = 0
  35.  
  36. For i As Integer = 0 To ds.Tables("customers2").Columns.Count - 1
  37. If i <> 2 Then
  38. new_dr(i) = dr(j)
  39. j += 1
  40. Else
  41. new_dr(i) = "ABC" '填入combobox的值
  42. End If
  43. Next
  44. ds.Tables("customers2").Rows.Add(new_dr)
  45. ds.Tables("customers").Rows.RemoveAt(index)
  46. End Sub
  47.  
  48. Private Sub DataGridView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragEnter
  49. e.Effect = DragDropEffects.Move
  50. End Sub
  51.  
  52. 'DataGridView2拖曳到DataGridView1
  53. Private Sub DataGridView2_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView2.CellMouseDown
  54. If e.Button = Windows.Forms.MouseButtons.Left Then
  55. index = e.RowIndex
  56. DataGridView2.DoDragDrop(ds.Tables("customers2").Rows(e.RowIndex), DragDropEffects.Move)
  57. End If
  58. End Sub
  59.  
  60. Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
  61. Dim dr As DataRow = e.Data.GetData(GetType(DataRow))
  62.  
  63. Dim new_dr As DataRow = ds.Tables("customers").NewRow
  64.  
  65. Dim j As Integer = 0
  66.  
  67. For i As Integer = 0 To ds.Tables("customers2").Columns.Count - 1
  68. If i <> 2 Then
  69. new_dr(j) = dr(i)
  70. j += 1
  71. End If
  72.  
  73. Next
  74. ds.Tables("customers").Rows.Add(new_dr)
  75. ds.Tables("customers2").Rows.RemoveAt(index)
  76. End Sub
  77.  
  78. Private Sub DataGridView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
  79. e.Effect = DragDropEffects.Move 
End Sub

沒有留言:

張貼留言