Results 1 to 3 of 3

Thread: copy selected datagridrow to new row

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    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.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width