none
Transfer datagridview to datagridview of another form RRS feed

  • Question

  • how to transfer datagridview content to datagridview of another form

    thank u in advance


    • Edited by no[one] Saturday, February 2, 2019 3:15 PM
    Saturday, February 2, 2019 2:12 PM

Answers

  • In the following example, the CloneColumns is only needed if the secondary form's DataGridView does not currently have columns.

    The CloneRowWithValues expects the secondary form's DataGridView to have the same amount of columns and that the column data types are the same and that neither DataGridView has it's DataSource set (which is why I originally asked about that and you didn't reply to that question).

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New Form2
        f.DataGridView1.CloneColumns(DataGridView1)
        For Each row As DataGridViewRow In DataGridView1.Rows
            row.CloneRowWithValues(f.DataGridView1, False)
        Next
        f.Show()
    End Sub

    Notes on above code

    • I assume that you are working from the main form to the secondary form rather than from the secondary form to then get the main form's data, if the other way around you would had need to specify that.

    Going the other direction we need to change the Show method to pass in the owner, Me is used.

    Then in the secondary form access the main form's DataGridView1

    Dim f As Form1 = CType(Owner, Form1)

    Now you can get to the main form's DataGridView1 and use the method I presented above to populate the secondary form's DataGridView.

    The code above requires a code module below.

    Module DataGridViewExtensions
    
        <DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Sub CloneRowWithValues(
            copyFromRow As DataGridViewRow,
            targetDataGridView As DataGridView,
            moveRow As Boolean)
    
            Dim resultRow As DataGridViewRow = CType(copyFromRow.Clone(), DataGridViewRow)
    
            For row As Integer = 0 To copyFromRow.Cells.Count - 1
                resultRow.Cells(row).Value = copyFromRow.Cells(row).Value
            Next
    
            targetDataGridView.Rows.Add(resultRow)
            If moveRow Then
                copyFromRow.DataGridView.Rows.Remove(copyFromRow)
            End If
    
    
        End Sub
        <Runtime.CompilerServices.Extension()>
        Public Sub CloneColumns(self As DataGridView, cloneFrom As DataGridView)
    
            If self.ColumnCount = 0 Then
                For Each c As DataGridViewColumn In cloneFrom.Columns
                    self.Columns.Add(CType(c.Clone, DataGridViewColumn))
                Next
            End If
        End Sub
    
    End Module



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, February 2, 2019 3:39 PM
    Moderator

All replies

  • Questions

    • Are both DataGridView controls have data inputted by the DataGridView rows or by a DataSource?
    • Do both of them have the same amount of columns?
    • Are all columns in question the same data type?
    • Do you want to transfer all rows or some rows?

    Here is a code sample that may help

    https://code.msdn.microsoft.com/DataGridView-copymove-rows-6f9ca7e8?redir=0


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, February 2, 2019 2:28 PM
    Moderator
  • all the columns has same data type and i want to transfer all the rows or content of my datagridview
    Saturday, February 2, 2019 3:20 PM
  • Are more than two Forms in one Project/Solution, i.e. in the same application?
    If these Forms are in the same application, you can copy one DataGridView to another.
    (I guess you want to transfer/copy all rows and all columns from original to another.)

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Saturday, February 2, 2019 3:27 PM
  • In the following example, the CloneColumns is only needed if the secondary form's DataGridView does not currently have columns.

    The CloneRowWithValues expects the secondary form's DataGridView to have the same amount of columns and that the column data types are the same and that neither DataGridView has it's DataSource set (which is why I originally asked about that and you didn't reply to that question).

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New Form2
        f.DataGridView1.CloneColumns(DataGridView1)
        For Each row As DataGridViewRow In DataGridView1.Rows
            row.CloneRowWithValues(f.DataGridView1, False)
        Next
        f.Show()
    End Sub

    Notes on above code

    • I assume that you are working from the main form to the secondary form rather than from the secondary form to then get the main form's data, if the other way around you would had need to specify that.

    Going the other direction we need to change the Show method to pass in the owner, Me is used.

    Then in the secondary form access the main form's DataGridView1

    Dim f As Form1 = CType(Owner, Form1)

    Now you can get to the main form's DataGridView1 and use the method I presented above to populate the secondary form's DataGridView.

    The code above requires a code module below.

    Module DataGridViewExtensions
    
        <DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Sub CloneRowWithValues(
            copyFromRow As DataGridViewRow,
            targetDataGridView As DataGridView,
            moveRow As Boolean)
    
            Dim resultRow As DataGridViewRow = CType(copyFromRow.Clone(), DataGridViewRow)
    
            For row As Integer = 0 To copyFromRow.Cells.Count - 1
                resultRow.Cells(row).Value = copyFromRow.Cells(row).Value
            Next
    
            targetDataGridView.Rows.Add(resultRow)
            If moveRow Then
                copyFromRow.DataGridView.Rows.Remove(copyFromRow)
            End If
    
    
        End Sub
        <Runtime.CompilerServices.Extension()>
        Public Sub CloneColumns(self As DataGridView, cloneFrom As DataGridView)
    
            If self.ColumnCount = 0 Then
                For Each c As DataGridViewColumn In cloneFrom.Columns
                    self.Columns.Add(CType(c.Clone, DataGridViewColumn))
                Next
            End If
        End Sub
    
    End Module



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, February 2, 2019 3:39 PM
    Moderator