472,331 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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
6 2255
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Faith | last post by:
Hello. I need to take a column from Excel(unknown amount of rows) that will be selected by the user and copy those cells. Then I will need to...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard...
5
by: kirschpe | last post by:
I developed an application using visual studio 2003. Now I want to make a similar application without having to redesign similar forms. Is there a...
0
by: Tom | last post by:
I need to be able to copy and paste data from an Excel spreadsheet to a grid or something to a web form and then save it to the database. example:...
1
by: veera372 | last post by:
i wants to copy a data from an excel and paste it into a data grid in the web form (asp .net) by clicking the button paste... i think it can be done...
2
by: veera372 | last post by:
hi this is veera, i wants to copy a data from an excel and paste it into a data grid in the web form (asp .net) by clicking the button paste... i...
1
by: veera372 | last post by:
hi this is veera, i wants to copy a data from an excel and paste it into a data grid in the web form (asp .net) by clicking the button paste... i...
0
by: veera372 | last post by:
hi this is veera, i wants to copy a data from an excel and paste it into a data grid in the web form (asp .net) by clicking the button paste... i...
2
by: OfficeDummy | last post by:
Hi, everyone! Like I mentioned in the thread title, I need to copy&paste data between different workbooks, and it works fine. However, when the...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.