Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

Datagridview copy and paste row data to another row

Hi Experts,

Software:
VB.Net 2005
SQL Server 2005

Is it possible to copy and paste row data from one row to another in a Datagridview?

If so, sample code would be appreciated.

Thanks,

Dave
Avatar of mytonytiger
mytonytiger

Yes it's possible, and not too hard to do either. I will post an example in a few minutes. :)
Public Class Form1

    Dim dt1 As DataTable
    Dim dt2 As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
        BuildDataTables()
        AddRows(dt1)
        Me.DataGridView1.DataSource = dt1
    End Sub

    Private Sub BuildDataTables()
        dt1 = New DataTable
        dt2 = New DataTable
        dt1.Columns.Add("Date", GetType(System.DateTime))
        dt1.Columns.Add("FirstName", GetType(System.String))
        dt1.Columns.Add("LastName", GetType(System.String))
    End Sub

    Private Sub AddRows(ByVal dt As DataTable)
        dt.Rows.Add(Now, "John", "Doe")
        dt.Rows.Add(Now, "Billy", "Goat")
        dt.Rows.Add(Now, "Tommy", "Boy")

    End Sub

    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.Control Then

            If e.KeyCode = Keys.C Then
                CopyRows()
                e.Handled = True
            End If
            If e.KeyCode = Keys.V Then
                pasterows()
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub CopyRows()
        If DataGridView1.SelectedRows.Count > 0 Then
            Dim row As DataGridViewRow
            row = DataGridView1.SelectedRows(0)
            Clipboard.SetDataObject(Me.DataGridView1.GetClipboardContent())
        End If
    End Sub

    Private Sub pasterows()
        If Clipboard.ContainsText Then
            Dim CopyText As String
            CopyText = Clipboard.GetText
            Dim Items() As String
            Items = CopyText.Split(ChrW(Keys.Tab))
            dt1.Rows.Add(Items)
        End If
    End Sub

End Class


Forget about the dt2 variable... when I first started, I was thinking about pasting between datagridviews, and not within a single one.
Avatar of David Svedarsky

ASKER

Hi,
Thanks for the response.

I am having some difficulty with this:

Private Sub AddRows(ByVal dt As DataTable)
      dt.Rows.Add(Now, "John", "Doe")
      dt.Rows.Add(Now, "Billy", "Goat")
      dt.Rows.Add(Now, "Tommy", "Boy")
   End Sub

I get these errors on any column Data Type other than String:

Invalid cast from 'DateTime' to 'Int32'.Couldn't store
Invalid cast from 'DateTime' to 'Decimal'.Couldn't store
Invalid cast from 'DateTime' to 'Double'.Couldn't store

When I comment out the columns with Data Types other than String, I get this error:

Input array is longer than the number of columns in this table.
===========================================
 
My columns & Data Types:

Private Sub BuildDataTables()
      dt1 = New DataTable
      dt1.Columns.Add("UpgradesKey", GetType(System.Int32))
      dt1.Columns.Add("Quantity", GetType(System.Decimal))
      dt1.Columns.Add("BasicDescription", GetType(System.String))

      dt1.Columns.Add("Description", GetType(System.String))
      dt1.Columns.Add("UnitPrice", GetType(System.Decimal))
      dt1.Columns.Add("Markup", GetType(System.Double))

      dt1.Columns.Add("Credit", GetType(System.Decimal))
      dt1.Columns.Add("Labor", GetType(System.Decimal))
      dt1.Columns.Add("EXT", GetType(System.Decimal))

   End Sub

Thanks,

Dave
ASKER CERTIFIED SOLUTION
Avatar of mytonytiger
mytonytiger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Make sure too that the number of items in Items <= the number of columns in your grid.
mytonytiger,

I can copy from the Datagridview and paste to the Notepad ok.

When I try to copy from and & paste to the same Datagridview I get this error:

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
=============================================
I assume this means that this code won't work on a *bound* Datagridview?

I realize that I didn't specify that the Datagridview was bound, but can you help?

Thanks,

Dave
mytonytiger,

Thanks for your help,

Dave
@mytonytiger

I am not able to paste the row at a desired location.. it always pastes the row to the bottom of the grid. Also is there a way by which a whole row can be cut? I'd also prefer doing a cut paste option.
Not much time this morning, but the cut code could be the same as the copy, just remove the row after cutting.

DataGridView1.Rows.Remove(row)

This is a very simplified example that is only meant to point you in the right direction. There would need to be additional code added to insert at a specific location, to cut without removing (graying out) until the paste was complete, validating that the data is compatible when pasting into another grid, etc... It really depends on the application and the needs of the users.