467,166 Members | 1,044 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

How to Copy/Paste existing row to a new row in data grid

Is there any way to copy an entire row from the Data Grid and then Paste it
into a new row, modify some data and then save it back to the database?

This grid is bound to a dataadapter with a commandbuilder monitoring the
activity.

Is there some property setting that needs to be set or what?
Nov 21 '05 #1
  • viewed: 2018
Share:
6 Replies
Darrel,

Your datagrid is only showing data in the daatatable, often while there are
filters in between as the dataview for the rows or the datatablestyle for
the columns.

However the datatable is the point you have to look for.

If you don't know how to get a currentrow, than reply where you show than
the row of the datasource, and tell what that is.

The newrow should at least have a proper new primary key to be able to add
it to the database.

I hope this helps?

Cor
Nov 21 '05 #2
"Darrell Wesley" <Da***********@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.com...
Is there any way to copy an entire row from the Data Grid and then Paste
it
into a new row, modify some data and then save it back to the database?

This grid is bound to a dataadapter with a commandbuilder monitoring the
activity.

Is there some property setting that needs to be set or what?

The following code does this sort of thing, I was pushed for time when I
wrote it so it may not be particularly elegant. It does work..
============
Private Sub btnCopy_Click(ByVal o As Object, ByVal e As
System.EventArgs) Handles btnCopy.Click
' This is quite tricky...
' EG you need a unique label for a bucket rule
' Then.... how about if the user gets to see the thing he just
appended
' and you can't just stick the data in the tentative add row,
oh no...
If dsBucket_Rules.HasChanges() Then
MsgBox("Please update your outstanding changes before copying")
Exit Sub
End If

Dim al As New ArrayList
Dim cm As CurrencyManager =
Me.BindingContext(Me.grdBucket_Rules.DataSource,
Me.grdBucket_Rules.DataMember)
Dim i As Integer
Dim FromRow As Integer
Dim dv As DataView = CType(cm.List, DataView)
For i = 0 To dv.Count - 1
If Me.grdBucket_Rules.IsSelected(i) Then
al.Add(i)
FromRow = i
End If
Next i
If al.Count < 1 Then
MsgBox("You must select a row first")
Exit Sub
End If
If al.Count > 1 Then
MsgBox("You can only copy one row at a time." & vbCrLf & "This
is because description must be unique when you leave a row.")
Exit Sub
End If
Dim numRows As Integer =
Me.grdBucket_Rules.BindingContext(Me.grdBucket_Rul es.DataSource,
Me.grdBucket_Rules.DataMember).Count

cm.EndCurrentEdit()

'Use the NewRow to create a DataRow in the DataSet
Dim myrow As DataRow = dsBucket_Rules.Tables(0).NewRow()

myrow("Description") = "** Copied Rule **"
myrow("Process_On") = Me.grdBucket_Rules.Item(FromRow, 1)
myrow("ProductLine") = Me.grdBucket_Rules.Item(FromRow, 2)
myrow("SiteStage") = Me.grdBucket_Rules.Item(FromRow, 3)
myrow("Site") = Me.grdBucket_Rules.Item(FromRow, 4)
If Me.grdBucket_Rules.Item(FromRow, 5) = False Then
myrow("LOF") = "N"
Else
myrow("LOF") = "Y"
End If
If Me.grdBucket_Rules.Item(FromRow, 6) = False Then
myrow("Special") = "N"
Else
myrow("Special") = "Y"
End If
If Me.grdBucket_Rules.Item(FromRow, 7) = False Then
myrow("Membrane") = "N"
Else
myrow("Membrane") = "Y"
End If
myrow("Dimension") = Me.grdBucket_Rules.Item(FromRow, 8)
myrow("Dimension_From") = Me.grdBucket_Rules.Item(FromRow, 9)
myrow("Dimension_To") = Me.grdBucket_Rules.Item(FromRow, 10)
myrow("Qty_Factor") = Me.grdBucket_Rules.Item(FromRow, 11)
myrow("Qty_Add") = Me.grdBucket_Rules.Item(FromRow, 12)
myrow("Qty_Cap_In") = Me.grdBucket_Rules.Item(FromRow, 13)
myrow("Warp_Set") = Me.grdBucket_Rules.Item(FromRow, 14)
Try
dsBucket_Rules.Tables(0).Rows.Add(myrow)
Catch ex As Exception
MsgBox(ex.Message)
End Try
' Avoid concurrency errors from btnUpdate
myrow.EndEdit()
cm.EndCurrentEdit()
cm.Refresh()
' move to the newly added row
cm.Position = cm.Count - 1
btnCopy.Visible = False
End Sub
=============
Private Sub grdBucket_Rules_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles grdBucket_Rules.MouseUp
' Hide the copy button if they have not selected a row
Dim pt = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = grdBucket_Rules.HitTest(pt)
If hti.Type = DataGrid.HitTestType.RowHeader Then
btnCopy.Visible = True
Else
btnCopy.Visible = False
End If
End Sub
=========
--
Regards,
Andy O'Neill
Nov 21 '05 #3
Cor and Andy,

I know that I can copy data out of any row that I click on but why can I not
do a simple paste into the newrow of the datagrid? I was hoping that it was
something that I was overlooking.

Why doesn't the data grid allow a paste of an entire row? It has a new row
entry at the bottom and you can certainly type data into it and then post it
back to the database but why won't a simple paste work?

"Cor Ligthert" wrote:
Darrel,

Your datagrid is only showing data in the daatatable, often while there are
filters in between as the dataview for the rows or the datatablestyle for
the columns.

However the datatable is the point you have to look for.

If you don't know how to get a currentrow, than reply where you show than
the row of the datasource, and tell what that is.

The newrow should at least have a proper new primary key to be able to add
it to the database.

I hope this helps?

Cor

Nov 21 '05 #4
Darrel,
Why doesn't the data grid allow a paste of an entire row? It has a new row
entry at the bottom and you can certainly type data into it and then post
it
back to the database but why won't a simple paste work?

I thought that I wrote that?

To what do you want to paste when it is set by a datagridtablestyle and all
values are from another type?

Cor
Nov 21 '05 #5
The datagrid is showing data from a single dataset and there is only one
table in the dataset. What I want to do "simply" is to copy an entire
row(Control-C will do this and you can paste the data into Notepad) and paste
it into the new row area of the grid - change the values that need to be
changed and then post it back to the underlying database. This would be just
like you can do in Access or Excel, but it doesn't appear to have paste
functionality for an entire row only for an individual cell.

"Cor Ligthert" wrote:
Darrel,
Why doesn't the data grid allow a paste of an entire row? It has a new row
entry at the bottom and you can certainly type data into it and then post
it
back to the database but why won't a simple paste work?

I thought that I wrote that?

To what do you want to paste when it is set by a datagridtablestyle and all
values are from another type?

Cor

Nov 21 '05 #6
Darrel,

Beside if it is possible, a datagrid is not office.

However what you want to do with the primary key?

Just creating a little button or menu item or context menu with copynew,
does in my opinion the same as that while you now let your users first copy
it and than paste it because that is the way it is done in office.

Cor
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Faith | last post: by
7 posts views Thread by lgbjr | last post: by
5 posts views Thread by kirschpe@hotmail.com | last post: by
reply views Thread by Tom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.