Visual Basic 2005, how to drag and drop between datagridviews

I am developing with visual basic 2005 and access 2007.

What I have is an access table that has full pathes to the pdf files located in 1 regular folder on my drive.  
So,I have an access table called "files" with "c:\files\main.pdf" pathes.
In run time I have a datagridview on the form that populates the names of these pdf files and excludes the entire path except the name in code.  What I need is to be able to grad a pdf file out  of this datagridview while maintaining the path to a particular pdf file and grad into another datagridview.  

Can someone point me into a right direction.
Private Sub Populate_All(ByVal FileName As String)
        DataGridView1.Columns.Clear()
        OpenDB()
        FileName = "'%" & FileName & "%'"
        Dim Sql As String = "SELECT Files FROM FileCondition"
        Dim cmd As New OleDbCommand(Sql, Conn)
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim dt As DataTable
        da.SelectCommand = cmd
        Try
            da.Fill(ds, "Data")
            dt = ds.Tables("Data")
            'DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = dt
            DataGridView1.Columns(0).HeaderText = "Reports"
            For Each dgrow As DataGridViewRow In DataGridView1.Rows
                If Not IsDBNull(dgrow.Cells("Files").Value) AndAlso dgrow.Cells("Files").Value IsNot Nothing Then
                    dgrow.Cells("Files").Value = dgrow.Cells("Files").Value.ToString.Replace("files\",").Replace(".pdf", "")
                End If
            Next
        Catch ex As Exception
            MsgBox("Error while executing: (" & cmd.CommandText & ")" & vbCrLf & "Msg: " & ex.Message.ToString, MsgBoxStyle.Exclamation, "Error")
        Finally
            da = Nothing
            ds = Nothing
            dt = Nothing
            cmd.Dispose()
            CloseDB()
        End Try
    End Sub

Open in new window

LVL 1
systems_axAsked:
Who is Participating?
 
planoczConnect With a Mentor Commented:
Here is a sample on how to drag and drop a datagrid..
Option Strict Off
Option Explicit On 
 
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
    Inherits Form
    Private WithEvents dataGrid1 As Form1.DnDDataGrid
    Private WithEvents dataGrid2 As Form1.DnDDataGrid
    Private components As Container
    Private WithEvents dt As DataTable
    Private WithEvents dt2 As DataTable
    'Fields
    'Constructors
    'Events
    'Methods
    'Nested Types
    Public Sub New()
        MyBase.New()
        Me.dt = New DataTable("Table1")
        Me.dt2 = New DataTable("Table2")
        '
        ' Required for Windows Form Designer support
        '
        InitializeComponent()
        '
        ' TODO: Add any constructor code after InitializeComponent call
        '
        dt.Columns.Add("Int32", Type.GetType("System.String"))
        dt.Columns.Add("Char", Type.GetType("System.String"))
        Dim dr As DataRow
        Dim i As Integer
        i = 0
 
        Do While (i <= 10)
            dr = dt.NewRow
            dr("Int32") = "text " + i.ToString
            dr("Char") = ChrW(i + 33)
            dt.Rows.Add(dr)
            i = (i + 1)
 
        Loop
        dataGrid1.DataSource = dt
        dt2.Columns.Add("Int32", Type.GetType("System.String"))
        dt2.Columns.Add("Char", Type.GetType("System.String"))
        Dim dr1 As DataRow
        i = 0
 
        Do While (i <= 20)
            dr1 = dt2.NewRow
            dr1("Int32") = "row " + i.ToString
            dr1("Char") = ChrW(i + 33)
            dt2.Rows.Add(dr1)
            i = (i + 1)
 
        Loop
        dataGrid2.DataSource = dt2
 
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
 
        If disposing Then
            If (Not (components) Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
 
    End Sub
    Private Sub InitializeComponent()
 
        Me.dataGrid1 = New DataGridDnD.Form1.DnDDataGrid
        Me.dataGrid2 = New DataGridDnD.Form1.DnDDataGrid
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.dataGrid2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        ' 
        ' dataGrid1
        ' 
        Me.dataGrid1.AllowDrop = True
        Me.dataGrid1.DataMember = ""
        Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid1.Location = New System.Drawing.Point(56, 24)
        Me.dataGrid1.Name = "dataGrid1"
        Me.dataGrid1.PreferredColumnWidth = 50
        Me.dataGrid1.Size = New System.Drawing.Size(208, 184)
        Me.dataGrid1.TabIndex = 0
        ' 
        ' dataGrid2
        ' 
        Me.dataGrid2.AllowDrop = True
        Me.dataGrid2.DataMember = ""
        Me.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid2.Location = New System.Drawing.Point(304, 24)
        Me.dataGrid2.Name = "dataGrid2"
        Me.dataGrid2.PreferredColumnWidth = 50
        Me.dataGrid2.Size = New System.Drawing.Size(224, 184)
        Me.dataGrid2.TabIndex = 1
        ' 
        ' Form1
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(584, 253)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dataGrid2, Me.dataGrid1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        AddHandler Me.Load, New System.EventHandler(AddressOf Form1_Load)
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.dataGrid2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
 
    End Sub
    <STAThread()> _
    Public Shared Sub Main()
 
        Application.Run(New Form1)
 
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 
 
    End Sub
    Public Class DnDDataGrid
        Inherits DataGrid
        Private dndMouseDownRow As Integer
        Private dndMouseDownCol As Integer
        'Fields
        'Constructors
        'Events
        'Methods
        Public Sub New()
            MyBase.New()
            AddHandler Me.DragOver, New System.Windows.Forms.DragEventHandler(AddressOf HandleDragOver)
            AddHandler Me.DragDrop, New System.Windows.Forms.DragEventHandler(AddressOf HandleDragDrop)
            AddHandler Me.DragEnter, New System.Windows.Forms.DragEventHandler(AddressOf HandleDragEnter)
            AddHandler Me.MouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseDown)
            AddHandler Me.MouseMove, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseMove)
            AddHandler Me.MouseUp, New System.Windows.Forms.MouseEventHandler(AddressOf HandleMouseUp)
            Me.dndMouseDownRow = -(1)
            Me.dndMouseDownCol = -(1)
            Me.AllowDrop = True
 
        End Sub
        Private Sub HandleMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
 
            Me.dndMouseDownRow = -(1)
            Me.dndMouseDownCol = -(1)
 
        End Sub
        Private Sub HandleMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
 
            Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
            ht = Me.HitTest(New Point(e.X, e.Y))
            Me.dndMouseDownRow = ht.Row
            Me.dndMouseDownCol = ht.Column
 
        End Sub
        Private Sub HandleMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
 
            Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
            ht = Me.HitTest(New Point(e.X, e.Y))
            If (e.Button = MouseButtons.Left) Then
                'Console.WriteLine(ht.ToString() );
                If (((dndMouseDownRow = -(1)) _
                            OrElse (dndMouseDownCol = -(1))) _
                            OrElse ((ht.Row = dndMouseDownRow) _
                            AndAlso (ht.Column = dndMouseDownCol))) Then
                    Return
                End If
                Dim data As [String]
                data = Me(Me.dndMouseDownRow, Me.dndMouseDownCol).ToString
                'Console.WriteLine(ht.ToString() + "\n" + data);
                Me.DoDragDrop(data, DragDropEffects.Copy)
                Me.dndMouseDownRow = -(1)
                Me.dndMouseDownCol = -(1)
            End If
 
        End Sub
        Private Sub HandleDragOver(ByVal sender As Object, ByVal e As DragEventArgs)
 
            Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
            ht = Me.HitTest(Me.PointToClient(New Point(e.X, e.Y)))
            'Console.WriteLine(ht.ToString());
            If (((ht.Row = Me.dndMouseDownRow) _
                        AndAlso (ht.Column = Me.dndMouseDownCol)) _
                        OrElse Not (e.Data.GetDataPresent("Text"))) Then
                e.Effect = DragDropEffects.None
            Else
                e.Effect = DragDropEffects.Copy
            End If
 
        End Sub
        Private Sub HandleDragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
 
            If e.Data.GetDataPresent("Text") Then
                e.Effect = DragDropEffects.Copy
            End If
 
        End Sub
        Private Sub HandleDragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
 
            'DataGrid dg = sender as DataGrid;
            'if(dg == null) return;
            If e.Data.GetDataPresent("Text") Then
                Dim ht As System.Windows.Forms.DataGrid.HitTestInfo
                ht = Me.HitTest(Me.PointToClient(New Point(e.X, e.Y)))
                If ((ht.Row = -(1)) _
                            OrElse (ht.Column = -(1))) Then
                    Return
                Else
                    Me(ht.Row, ht.Column) = CType(e.Data.GetData("Text"), String)
                End If
            End If
        End Sub
    End Class
End Class

Open in new window

0
 
systems_axAuthor Commented:
Hi,
I am trying to drag the path to the document from 1 datagrid and into another one though and I do not understand your code, sorry.
thanks
0
 
systems_axAuthor Commented:
is it possible for you to attach or upload the application so that I can see what the code is doing as I do not understand this code?
thank you
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
planoczCommented:
Just open a new VB.net winform app. Then copy this code into the new form1.
then run app. you will see that you can drag an drop.
0
 
systems_axAuthor Commented:
i am not able to simply create a form, put your code in, and run it without errors.
0
 
planoczCommented:
What are the errors?
0
 
systems_axAuthor Commented:
on these 2 lines:

1) Private Sub InitializeComponent()

        Me.dataGrid1 = New DataGridDnD.DragDrop.DnDDataGrid
        Me.dataGrid2 = New DataGridDnD.DragDrop.DnDDataGrid
**says it is not defined

2)  under the " Private Sub HandleMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)"
If (e.Button = MouseButtons.Left) Then

***had to change it to :
If (e.Button = Windows.Forms.MouseButtons.Left) Then

I cannot open the designer as there are 4 errors that are generated:
Could not find type 'CrystalreportTest.DragDrop.DnDDataGrid'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.  
 

Could not find type 'CrystalreportTest.DragDrop.DnDDataGrid'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.  
 

The variable 'dataGrid2' is either undeclared or was never assigned.
Hide    Edit

at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)

The variable 'dataGrid1' is either undeclared or was never assigned.
 
can you upload your file with this code so that i could take a look at it please?
0
 
planoczCommented:
The code above is a complete code. So it should work for you.
But it looks like you would need to go ahead and add the two datagrids to your form so that the code can see it and make it defined.
0
 
systems_axAuthor Commented:
thank you,
I will try it out.
0
 
systems_axAuthor Commented:
planocz,
I did and it does work, I will play with the code and try to figure out how to drag and drop within my setup.  
0
 
systems_axAuthor Commented:
planocz,
I apologize my testing is still in progress.
0
 
planoczCommented:
no problem Thanks
0
 
systems_axAuthor Commented:
hi planocz,
I have tried implemetning the code you have provided into mine and so far no good.
My datagridview3 is the grid with data, it does have "Reports" column added in design time and does display text data.  My datagridview4 is the target and has also the column called "Reports" and it is not bound.  I am able to drag from the source grid but nothing drops into the target grid.
do you see anything wrong with my code.
thank you

Private Sub DataGridView4_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView4.DragEnter
        If e.Data.GetDataPresent(GetType(DataGridView)) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
 
 
 Private Sub DataGridView3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView3.MouseDown
        DataGridView3.DoDragDrop(DataGridView3, DragDropEffects.Copy)
    End Sub
 
 
 
 Private Sub DataGridView4_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView4.DragDrop
 
        If e.Data.GetDataPresent(GetType(DataGridView)) Then
 
            Dim lnk As DataGridView = CType(e.Data.GetData(GetType(DataGridView)), DataGridView)
 
            Dim bs As BindingSource = CType(DataGridView4.DataSource, BindingSource)
            Dim dr As DataRow = CType(bs.DataSource, DataSet).Tables(0).NewRow
            dr("Reports") = lnk.Text ' Where [text] is the column you're binding...
             CType(bs.DataSource, DataSet).Tables(0).Rows.Add(dr)
 
        End If
 
    End Sub

Open in new window

0
 
systems_axAuthor Commented:
planocz,
can you help me out?
0
All Courses

From novice to tech pro — start learning today.