2

The following code loads a tab delimited file into my DataGridView (loads 1 record from the data file). All this works perfectly, however, I need to duplicate this record X amount of times. Once the row is duplicated, I'll need to eventually edit some fields and write to a new file with the rows added.

I'v tried adding rows dynamically but it yells at me saying I can't because the data is bound.

Suggestions?

    Dim file As String = "Temp.txt"
    Dim path As String = "C:\Temp\"
    Dim ds As New DataSet
    Dim tbl As New DataTable

    Try
        If IO.File.Exists(IO.Path.Combine(path, file)) Then
            Dim ConStr As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
            path & ";Extended Properties=""Text;HDR=No;FMT=TabDelimited\"""
            Dim conn As New OleDb.OleDbConnection(ConStr)
            Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
            file, conn)
            da.Fill(ds, "TextFile")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

    DataGridView1.DataSource = ds.Tables(0)
Muhnamana
  • 1,014
  • 13
  • 34
  • 57

2 Answers2

1

You cannot add directly to a DataGridView which is databound because the data resides elsewhere and the DGV is simply displaying what is there. To add rows to a DataTable:

Dim dr = ds.Tables(0).NewRow()

This will create a new row with the columns defined for it based on the table. Add the data for the new item to it, then add it to the table:

dr.Item(0) = "something"   ' add to column one
... etc
ds.Tables(0)Rows.Add(dr)

You did not really need to create a DataSet for what you have so far. Alternative:

Private myDT As New DataTable      
...
da.Fill(myDT)

To literally clone the data for a row:

Dim dr As DataRow

For n As Integer = 0 To NumberOfDuplicates
    dr = myDT.NewRow              ' new DataRow
    For c As Integer = 0 To myDT.Columns.Count - 1   ' copy data from 0 to NewRow
        dr.Item(c) = myDT.Rows(0).Item(c)
    Next
    myDT.Rows.Add(dr)            ' add NewRow to datatable
Next n

Note that you need to create a new row for each clone, the inner loop copies the data from row(0) to each new one.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • the file has a ton of fields, 100+ and id had to define each field. there's no way to duplicate the entire row? – Muhnamana Jun 24 '15 at 17:08
  • i think im in waaaay over my head on this, still learning the rope and confused where the code for the clone needs to be placed in relationship to when the table is passed to the datagridview. i need to take a step back on this... – Muhnamana Jun 24 '15 at 17:22
  • you'd only be able to clone a row *after* you've loaded the data. but anytime after that is fine. As you add rows to the DT they will just show up in the DGV – Ňɏssa Pøngjǣrdenlarp Jun 24 '15 at 17:24
  • i added the dup before the initial load, which does dup but once the initial load is complete, i'm not sure how to dup and reload per say. – Muhnamana Jun 24 '15 at 17:27
  • 1
    there is no need to "reload" when you add some clones they will show up in the DGV and be a part of the datatable. If you want to add at different times, your datatable should probably be a form level variable (private... not Dim in a procedure) – Ňɏssa Pøngjǣrdenlarp Jun 24 '15 at 17:28
0
'Hope This helps DGV1 is the datagridview
'To copy Row
Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
End Sub

'To Paste Row
Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
    PasteRowIndex = DGV1.CurrentRow.Index
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next

End Sub

'To Duplicate Rows
Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
    CopyRowIndex = DGV1.CurrentRow.Index
    DGV1.Rows.Add()
    DuplicateRowIndex = DGV1.Rows.Count - 1
    For index As Int32 = 0 To DGV1.ColumnCount - 1
        DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
    Next
End Sub
Hanish
  • 21
  • 2