Solved

VB.net DataGridView drag multiple items

Posted on 2013-01-25
6
1,271 Views
Last Modified: 2013-01-27
Hi. I use the following code to drag a single item from DataGridView1 to DataGridView2.
How do I update the code so that multiple items can be dragged


    Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        Try

            Dim info As DataGridView.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
            Dim row As DataGridViewRow = Me.DataGridView1.Rows(info.RowIndex)

            Me.DataGridView1.DoDragDrop(row, DragDropEffects.Copy)

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

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

        Catch
        End Try
    End Sub

    Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop


        Try

            Dim row As DataGridViewRow = TryCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
            'This is where the row from the ist DGV is effectively copied and added to the second DGV
            If row IsNot Nothing Then

                Dim newRow As New DataGridViewRow()
                Dim oAmount As Decimal
                Dim oDue As Decimal

                newRow.CreateCells(Me.DataGridView1)
                '------------------------------------------------------------------
                'First get first three columns from DataGridView1

                newRow.Cells(0).Value = row.Cells(0).Value
                newRow.Cells(1).Value = row.Cells(1).Value
                newRow.Cells(2).Value = row.Cells(2).Value

                newRow.Cells(3).Value = Me.DateTimePicker_PaySelected.Value.Date.ToString("dd MMMM yyyy")
                'Now gather the Amount and Pad cell values in top DGV
                If IsNumeric(row.Cells(3).Value) = False Then
                    oAmount = 0
                Else
                    oAmount = row.Cells(3).Value
                End If
                If IsNumeric(row.Cells(4).Value) = False Then
                    oDue = 0
                Else
                    oDue = row.Cells(4).Value
                End If
                If oDue = 0 Then
                    MsgBox("This has been paid!")
                    Exit Sub
                End If

                newRow.Cells(4).Value = oDue

                newRow.Cells(5).Value = Me.ComboBox_PaymentSelected.Text

                'Me.DataGridView2.Columns(0).DefaultCellStyle.ForeColor = Color.Azure

                'next customize the
                Dim oSupplier As String = row.Cells(1).Value
                Dim oReference As String = row.Cells(2).Value

                If Me.DataGridView1.Rows(row.Index).DefaultCellStyle.ForeColor <> Color.OrangeRed _
                    Or Check_If_Supplier_And_Reference_in_DGV2(oSupplier, oReference) = False Then

                    Me.DataGridView2.Rows.Add(newRow) '
                Else
                    MsgBox("The invoice is already there!")

                    'Don't add the row if it is already owolintshi
                End If

            End If
            '***Color line orange -on the drop
            Me.DataGridView1.Rows(row.Index).DefaultCellStyle.ForeColor = Color.OrangeRed
            Me.DataGridView1.ClearSelection()
        Catch
            MsgBox(Err.Description)
        End Try

        Exit Sub
0
Comment
Question by:murbro
  • 3
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38818285
You can change your mousedown to following

 Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        Try

            Dim info As DataGridView.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
            Dim rows As DataGridViewRow() = Me.DataGridView1.SelectedRows

            Me.DataGridView1.DoDragDrop(rows, DragDropEffects.Copy)

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub

Open in new window




And change your dragdrop to

    Private Sub DataGridView2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView2.DragDrop


        Try

            Dim rows As DataGridViewRow() = TryCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
        

Open in new window



  You will need to wrap code from there after into a for loop so that each row is added
0
 

Author Closing Comment

by:murbro
ID: 38818539
Thanks very much
0
 

Author Comment

by:murbro
ID: 38818569
Hi. I got an error

1
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38818883
Change the type of rows to

DatagridviewSelectedRowsCollection in both methods.
0
 

Author Comment

by:murbro
ID: 38821475
Great! That did the trick. Thanks very much
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38824263
Glad its working :-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Single Form 8 42
Need a starter for ETL protocol? 4 55
get column names from table in vb.net 8 27
Help with preventing downloading a zip file 10 35
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question