copy selected datagridrow to new row
Greeting guys,
What I have is a inventory database setup as a datagrid bound to a datatable. Everything is working great, but what I would like to do is if at any point in time we let say, order 3 pieces of hardware on one purchase I would like to add one row (with a form for new hardware) then after that row is done and the datagrid has added it, I would like to right click on that selected row and copy it to the same datagrid. I can get it to copy but the serial of the new hardware is unique so I need to change that before it is copied.
Code:
Private Sub CntxtMnuCopy_Click(sender As Object, e As EventArgs) Handles CntxtMnuCopy.Click
Dim SelectedHardware As String
Dim NewValue As Object
SelectedHardware = DataGridView6.CurrentRow.Cells(1).Value
DataGridView6.EndEdit()
Dim TargetTable = DirectCast(SwitchesBindingSource.DataSource, DataTable)
Dim rows = DataGridView6.SelectedRows
NewValue = InputBox("Enter New Serial", "New Serial")
For Each row As DataGridViewRow In rows
If row.Index = 1 Then
DataGridView6.Cells(0).Value = NewValue.ToString
End If
TargetTable.ImportRow(CType(SwitchesBindingSource.Item(row.Index), DataRowView).Row)
Next
End Sub
The problem is I am not sure how to change the cell that holds the serial before it is added. Of course this line
Code:
DataGridView6.Cells(0)
doesn't work. Am I on the right path with this or is there an easier way? It would also need to be saved to the database as well.
Re: copy selected datagridrow to new row
I would probably go at it in reverse. I would copy the underlying datarow from the datatable that represents that row in the datagridview. Once you've copied the datarow, then you would change the serial number in that new row and then add it to the datatable and refresh the grid.
Not sure how the nuts and bolts of this would look off hand - just proposing a scenario off the top of my head. :)
Re: copy selected datagridrow to new row
Thanks dolot, I ended up going that way, basically.
Code:
Dim currentrow As DataRow = DirectCast(Me.DataGridView6.DataSource.Current, DataRowView).Row
NewValue = InputBox("Enter New Serial", "New Serial")
Dim row = Me.SwitchesBindingSource.AddNew
row("Serial") = Replace(CStr(NewValue), "'", "`")
row("Model") = CStr(currentrow("Model").ToString)
.......
A little more code but I guess that's ok.