David Svedarsky
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
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
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.ClipboardCop yMode = DataGridViewClipboardCopyM ode.Enable WithoutHea derText
BuildDataTables()
AddRows(dt1)
Me.DataGridView1.DataSourc e = 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(ByVa l sender As Object, ByVal e As System.Windows.Forms.KeyEv entArgs) 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 .DataGridV iew1.GetCl ipboardCon tent())
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.T ab))
dt1.Rows.Add(Items)
End If
End Sub
End Class
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.ClipboardCop
BuildDataTables()
AddRows(dt1)
Me.DataGridView1.DataSourc
End Sub
Private Sub BuildDataTables()
dt1 = New DataTable
dt2 = New DataTable
dt1.Columns.Add("Date", GetType(System.DateTime))
dt1.Columns.Add("FirstName
dt1.Columns.Add("LastName"
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(ByVa
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
Dim row As DataGridViewRow
row = DataGridView1.SelectedRows
Clipboard.SetDataObject(Me
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.T
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.
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("UpgradesK ey", GetType(System.Int32))
dt1.Columns.Add("Quantity" , GetType(System.Decimal))
dt1.Columns.Add("BasicDesc ription", GetType(System.String))
dt1.Columns.Add("Descripti on", 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
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("UpgradesK
dt1.Columns.Add("Quantity"
dt1.Columns.Add("BasicDesc
dt1.Columns.Add("Descripti
dt1.Columns.Add("UnitPrice
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Make sure too that the number of items in Items <= the number of columns in your grid.
ASKER
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
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
ASKER
mytonytiger,
Thanks for your help,
Dave
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.
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.
DataGridView1.Rows.Remove(
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.