2011年12月5日 星期一

datagridview 拖曳至另一datagridview

參考引用
--
'將DataGridView1及DataGridView2的AllowDrop屬性設為true

    Dim cn As New SqlConnection("server=192.168.7.23;database=northwind;user id=sa;password=sa")
    Dim cmd As New SqlCommand("select customerid,companyname,city from customers", cn)
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet

    Dim index As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        da.SelectCommand = cmd
        da.Fill(ds, "customers")

        cmd.CommandText = "select customerid,companyname,'' as temp,city from customers"
        da.FillSchema(ds, SchemaType.Source, "customers2")

        DataGridView1.DataSource = ds.Tables("customers")
        DataGridView2.DataSource = ds.Tables("customers2")
    End Sub

    'DataGridView1拖曳到DataGridView2
    Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            index = e.RowIndex
            DataGridView1.DoDragDrop(ds.Tables("customers").Rows(e.RowIndex), DragDropEffects.Move)
        End If
    End Sub

    Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop
        Dim dr As DataRow = e.Data.GetData(GetType(DataRow))

        Dim new_dr As DataRow = ds.Tables("customers2").NewRow

        Dim j As Integer = 0

        For i As Integer = 0 To ds.Tables("customers2").Columns.Count - 1
            If i <> 2 Then
                new_dr(i) = dr(j)
                j += 1
            Else
                new_dr(i) = "ABC" '填入combobox的值
            End If
        Next
        ds.Tables("customers2").Rows.Add(new_dr)
        ds.Tables("customers").Rows.RemoveAt(index)
    End Sub

    Private Sub DataGridView2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    'DataGridView2拖曳到DataGridView1
    Private Sub DataGridView2_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView2.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            index = e.RowIndex
            DataGridView2.DoDragDrop(ds.Tables("customers2").Rows(e.RowIndex), DragDropEffects.Move)
        End If
    End Sub

    Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
        Dim dr As DataRow = e.Data.GetData(GetType(DataRow))

        Dim new_dr As DataRow = ds.Tables("customers").NewRow

        Dim j As Integer = 0

        For i As Integer = 0 To ds.Tables("customers2").Columns.Count - 1
            If i <> 2 Then
                new_dr(j) = dr(i)
                j += 1
            End If

        Next
        ds.Tables("customers").Rows.Add(new_dr)
        ds.Tables("customers2").Rows.RemoveAt(index)
    End Sub

    Private Sub DataGridView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
        e.Effect = DragDropEffects.Move 
End Sub

沒有留言:

張貼留言