"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