将数据从 DG 和其他控件拖到 vb.net 中的另一个 DG [英] Drag data from DG and other controls to another DG in vb.net

查看:27
本文介绍了将数据从 DG 和其他控件拖到 vb.net 中的另一个 DG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB.Net 2010 中有表单:

I have form in VB.Net 2010:

我想在 dgRegister 和日期、课程 ID 中单击并拖动多行以放入 dgCourseStudent,其中包含日期、注册 ID、注册名称和课程 ID.

I want to click-drag multi rows in dgRegister and Date, Course ID to drop in dgCourseStudent with column Date, Register ID, Register Name and Course ID.

如何用 vb.net 语言编写代码?

How to code this in vb.net language?

推荐答案

首先将 dgCourseStudent 的 AllowDrop 属性设置为 True(它将接受拖动事件).我假设您使用的是 DataSet 或 DataTable,这里是我的示例:

First of all put the AllowDrop property of dgCourseStudent to True (it will accept the dragging events). I've presumed you're using DataSet or DataTable, here my example:

 Dim downHitInfo As DataGridView.HitTestInfo = Nothing 'Used to keep trace of dragging info

''MouseDown used to know is a DragDrop event is required
Private Sub dgRegister_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgRegister.MouseDown
    Dim view As DataGridView = CType(sender, DataGridView)
    Dim hitInfo As DataGridView.HitTestInfo = view.HitTest(e.X, e.Y)
    If Not Control.ModifierKeys = Keys.None Then
        Exit Sub
    End If
    If e.Button = MouseButtons.Left And hitInfo.RowIndex >= 0 Then
        downHitInfo = hitInfo
    End If
End Sub

''MouseMove used to know what DataRow is being dragged.
Private Sub dgRegister_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgRegister.MouseMove
    Dim view As DataGridView = CType(sender, DataGridView)
    If e.Button = MouseButtons.Left And Not downHitInfo Is Nothing Then
        Dim dragSize As Size = SystemInformation.DragSize
        Dim DragRect As Rectangle = New Rectangle(New Point(Convert.ToInt32(downHitInfo.ColumnX - dragSize.Width / 2), _
      Convert.ToInt32(downHitInfo.RowY - dragSize.Height / 2)), dragSize)
        If Not DragRect.Contains(New Point(e.X, e.Y)) Then
            'Extract the DataRow
            Dim gridRowView As DataGridViewRow = DirectCast(view.Rows(downHitInfo.RowIndex), DataGridViewRow)
            Dim rowView As DataRowView = DirectCast(gridRowView.DataBoundItem, DataRowView)

            'Raise the DragDrop with the extracted DataRow
            view.DoDragDrop(rowView.Row, DragDropEffects.Move)
            downHitInfo = Nothing
        End If
    End If
End Sub

'' For mouse cursor
Private Sub dgCourseStudent_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgCourseStudent.DragOver
    e.Effect = DragDropEffects.Move
End Sub

''The core of draggin procedure
Private Sub dgCourseStudent_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgCourseStudent.DragDrop
    'Retreive the dragged DataRow
    Dim draggedRow As DataRow = CType(e.Data.GetData(GetType(DataRow)), DataRow)
    ''
    '' Put your code here to insert the dragged row into dgCourseStudent grid
End Sub

这篇关于将数据从 DG 和其他控件拖到 vb.net 中的另一个 DG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆