Solved

Copying rows from DatagridView to Another DataGridView

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

22 Experts available now in Live!

Get 1:1 Help Now