Link to home
Start Free TrialLog in
Avatar of systems_ax
systems_ax

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of systems_ax
systems_ax

ASKER

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
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
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.
i am not able to simply create a form, put your code in, and run it without errors.
What are the errors?
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?
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.
thank you,
I will try it out.
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.  
planocz,
I apologize my testing is still in progress.
no problem Thanks
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

planocz,
can you help me out?