-4

I am binding a selected row to a new data row which needs to be added to a database.

I am using the dt.Importrow(newRow) to add the new datarow to the data table.

However, if I make changes to this new row, it also changes the selected row.

code below

'''             Dim original_Row As DataRow
                Dim newRow As DataRow = dt.NewRow   
original_Row = CType(DataTbale.CurrentRow.DataBoundItem, DataRowView).Row 

                newRow = original_Row

newRow("Name") = John 'Changing value to new row here

                dt.ImportRow(newRow)'''
Fault Spot
  • 3
  • 11

1 Answers1

0

I think that what you actually want to do is create a new row for a DataTable, copy data from the currently selected row, modify that data, then add the row to the table.

Firstly, you should be binding your DataTable via a BindingSource and getting the selection from that:

'Get the current row.
Dim currentRow = DirectCast(myBindingSource.Current, DataRowView).Row

'Create a new row.
Dim newRow = myDataTable.NewRow()

'Copy existing data to new row.
newRow.ItemArray = currentRow.ItemArray

'Edit data.
newRow("Name") = "John"

'Add new row to table.
myDataTable.Rows.Add(newRow)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46