Solved

Copying rows from DatagridView to Another DataGridView

Posted on 2006-11-03
4
601 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…

710 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