Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Copying rows from DatagridView to Another DataGridView

Posted on 2006-11-03
4
Medium Priority
?
617 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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