Solved

Copying rows from DatagridView to Another DataGridView

Posted on 2006-11-03
4
590 Views
Last Modified: 2010-04-23
I want to copy all the selected rows from a datagridview into another one.  The below errors saying the row is already part of another datagrid.  How do I go about this?

    Private Sub DataGridView1_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp

        DataGridView2.ColumnCount = DataGridView1.ColumnCount
        For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
            DataGridView2.Rows.Add(row)
        Next

    End Sub

Thanks in advance!
0
Comment
Question by:DavidGreenfield
  • 2
4 Comments
 
LVL 10

Expert Comment

by:gangwisch
ID: 17871395
 DataGridView2.ColumnCount = DataGridView1.ColumnCount
        For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
dim mynewrow as new DataGridViewRow
mynewrow=row          
DataGridView2.Rows.Add(mynewrow)
        Next
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17871398
oh yeah you need to also add the columns from the other table first
so the code would be:

       For Each col As DataGridViewColumn In Me.DataGridView1.Columns
dim mynewcol as new DataGridViewColumn
mynewcol=col
DataGridView2.Columns.Add(mynewcol)
        Next

       For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
dim mynewrow as new DataGridViewRow
mynewrow=row          
DataGridView2.Rows.Add(mynewrow)
        Next
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17872654
gangwisch

That won't work: for the same reason that the original code wouldn't work.  Neither a DataGridViewColumn nor a DataGridViewRow can belong to more than one DataGridView.

DavidGreenfield

Although (unlike with the earlier DataGrid) it is possible to have a DataGridView that is unbound, it is more often used bound to a datasource.  This means that it will only display data that is in that datasource - possibly filtered in some way.  It does this by using a DataView based on the datasource.  Even if the DataViewGrid is bound directly to a datatable the binding actually uses that datatable's .DefaultView.  If you want only some records from the datatable to display you do so by applying a .RowFilter to the dataview.  You can also, in effect, filter columns by e.g. setting some of them to .Visible = False.

It is possible to have more than one dataview, filtered in different ways, of the same datatable.  That, in my view, is what is needed in this scenario.  And, so that such filtering will work, the datatable needs to contain a column which can be set by the main datagridview so that it can be used as a filter by the other datagridview.  That column can be added, temporarily and programmatically, after the datatable has been created and filled from the database.

So try this.  One form, two DataGridViews - dgvMain and dgvSelected.  This code

    Dim dt As New DataTable("TestTable")
    Dim dvSelected As New DataView

    Private Sub filltable()

        'make a couple of columns
        Dim dc1 As New DataColumn("ID", GetType(Integer))
        dt.Columns.Add(dc1)
        Dim dc2 As New DataColumn("Name", GetType(String))
        dt.Columns.Add(dc2)
        'add some dummy data
        For i As Integer = 1 To 10
            Dim dr As DataRow = dt.NewRow
            dr("ID") = i
            dr("Name") = "Name" & i.ToString
            dt.Rows.Add(dr)
        Next

    End Sub

    Private Sub TestForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'get data - equivalent to filling the datatable from a database
        filltable()

        'make a "temporary" column
        Dim dc As New DataColumn("Selected", GetType(Boolean))
        dc.DefaultValue = False
        dt.Columns.Add(dc)

        'bind main grid
        dgvMain.DataSource = dt
        'hide temporary column
        dgvMain.Columns.Item("Selected").Visible = False

        'set up dataview for selection
        dvSelected.Table = dt
        'apply filter
        dvSelected.RowFilter = "Selected = True"
        'bind selected grid to it
        dgvSelected.DataSource = dvSelected
        'hide temporary column
        dgvSelected.Columns.Item("Selected").Visible = False

    End Sub

    Private Sub dgvMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvMain.MouseUp

        For Each row As DataGridViewRow In dgvMain.Rows
            If row.Selected Then
                row.Cells("Selected").Value = True
            Else
                row.Cells("Selected").Value = False
            End If
        Next
        BindingContext(dt).EndCurrentEdit()

    End Sub

You will need to adapt it to fit your set up, but it illustrates the approach.

Roger
0
 

Author Comment

by:DavidGreenfield
ID: 17873643
That works a treat!, Thank you very much Roger, not something I would of been able to guess!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now