Copy a row from one Datagrid to another Datagrid RRS feed

  • Question

  • Is their a way to copy a row or rows  from one Datagrid to another Datagrid. All columns match up. Im using vb 2005  and sql 2005.



    Thursday, November 15, 2007 4:41 PM

All replies

  • If this is a DataTable in the DataSource, you can copy the ItemArray of the DataRow you want.

    Thursday, November 15, 2007 5:24 PM
  • Basically i pull information from a Excel into a datagrid. I want to copy the rows from one datagrid to another.


    Here is my code  for this



                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim DtSet As System.Data.DataSet
                Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
                MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & _
                "data source='" & PrmPathExcelFile & " '; " & "Extended Properties=Excel 8.0;")
                'Select the data from Sheet1 of the workbook.
                MyCommand = New System.Data.OleDb.OleDbDataAdapter("select [F1] as Modality,[F2] as Status,[F3] as SoftwareRev, " & _
                "[F4] as HardwareLocation,[F5] as ClinicalLocation,[F6] as HostName,[F7] as DomainName, [F8] as DeviceType ,[F9] as PCType, [F10] as Memory, " & _
                "[F11] as ProductModel,[F12] as SerialNo,[F13] as HPSerialNo,[F14] as BiosVersion,[F15] as MacAddress,[F16] as IpAddress,[F17] as SubnetMask, " & _
                "[F18] as Gateway,[F19] as DNS1,[F20] as DNS2, [F21] as DNSSuffix,[F22] as WINSServer, [F23] as PrinterConnected,[F24] as DefaultPrinter, " & _
                "[F25] as Antivirus,[F26] as AntivirusSolution,[F27] as ImageLocation,[F28] as Imageowner,[F29] as ImageSolution,[F30] as ThirdPartyApp1, " & _
                "[F31] as ThirdPartyApp2,[F32] as ThirdPartyApp3 from [hardware$]", MyConnection)
                MyCommand.TableMappings.Add("Table", "Hardware")
                DtSet = New System.Data.DataSet
                DGView.DataSource = DtSet.Tables(0)
                MsgBox("Import complete", MsgBoxStyle.Information, "T.T.S. Online")

            Catch ex As Exception

            End Try


    It populates ok, I having trouble getting the information to the other datagrid, which is bound to a different dataset and a different table. So to avoid issues the the PK, the FK i figure i can copy the rows from the Datagrid A  into the datagrid  B and update the database using the table adapter.


    Can you show me a example? 

    Thursday, November 15, 2007 7:23 PM
  • Hi,

       If you want to copy and paste datagridview row,here is some code that will allow you to copy/paste text, say from Excel to a DataGridView or a DataGridView to a DataGridView. It uses the KeyDown event to catch the ctl+V and ctl+C.


    Code Block

               this.dataGridView1.KeyDown += new


            void dataGridView1_KeyDown(object sender, KeyEventArgs e)


                if (e.Control && e.KeyCode == Keys.C)


                    DataObject d = dataGridView1.GetClipboardContent();


                    e.Handled = true;


                else if(e.Control && e.KeyCode == Keys.V)


                    string s = Clipboard.GetText();

                    string[] lines = s.Split('\n');

                    int row = dataGridView1.CurrentCell.RowIndex;

                    int col = dataGridView1.CurrentCell.ColumnIndex;

                    foreach (string line in lines)


                        if (row < dataGridView1.RowCount && line.Length >0)


                            string[] cells = line.Split('\t');

                            for (int i = 0; i < cells.GetLength(0); ++i)


                                if (col + i <



                                    dataGridView1[col + i, row].Value =Convert.ChangeType(cells[i], dataGridView1[col + i, row].ValueType);


















    For more information , please check following articles






    If you have any further questions ,feel free to let me know.

    Best Regards






    Monday, November 19, 2007 9:16 AM
  • Ok I incorporated this in my code Im having trouble in a certain area. It will copy the contents in the datagrid to a excel just fine. But when i want to copy back into the datagrid the same contents to a different row for testing. Nothing happens well i can tell the app is sort of hung cause it is in sort of a infinite loop or something.


    My app has one datagrid called "dgview" nothing major really for testing. Here is code you posted but converted to VB

    You can see in my comments where it is hanging.


    Private Sub Import_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    AddHandler Me.DGView.KeyDown, AddressOf dgview_KeyDown

    End Sub


    Sub dgview_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)

    If e.Control And e.KeyCode = Keys.C Then

    Dim d As DataObject = DGView.GetClipboardContent()


    e.Handled = True


    If e.Control And e.KeyCode = Keys.V Then

    Dim s As String = Clipboard.GetText()

    Dim lines As String() = s.Split(ControlChars.Lf)

    Dim row As Integer = DGView.CurrentCell.RowIndex

    Dim col As Integer = DGView.CurrentCell.ColumnIndex

    Dim line As String

    For Each line In lines

    If row < DGView.RowCount And line.Length > 0 Then

    Dim cells As String() = line.Split(ControlChars.Tab)

    Dim i As Integer



    'the Loop is here and it doesn't perform anything it just keeps looping back

    'This is where the breakpoint is at

    While i < cells.GetLength(0)

    If col + i < Me.DGView.ColumnCount Then

    DGView(col + i, row).Value = Convert.ChangeType(cells(i), DGView(col + i, row).ValueType)

    'I see the values in the Cell(i) it looks like it captured the values but nothing is adding to the rows



    Exit While

    End If

    End While

    row += 1


    Exit For

    End If

    Next line

    End If

    End If

    End Sub 'dgview_KeyDown

    End Class



    Tuesday, November 20, 2007 8:11 PM