473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2349
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 paste those cells into the first column in a Data...
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 was easy. But pasting an image from 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 way to copy and paste windows forms across...
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: if my spreadsheet has 20 rows of data I need to be...
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 in java script using clipboard option... but i...
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 think it can be done in java script using clipboard...
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 think it can be done in java script using clipboard...
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 think it can be done in java script using clipboard...
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 data has been copied to the destination workbook,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.