Solved

Copying rows from DatagridView to Another DataGridView

Posted on 2006-11-03
4
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 42
Access Schema 6 45
How to repeat the data 4 30
Web page design problem 3 14
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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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