-4

我将选定的行绑定到需要添加到数据库的新数据行。

我正在使用 dt.Importrow(newRow) 将新数据行添加到数据表中。

但是,如果我对此新行进行更改,它也会更改所选行。

下面的代码

'''             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 '在此处将值更改为新行

                dt.ImportRow(newRow)'''
4

1 回答 1

0

我认为您真正想要做的是为 a 创建一个新行DataTable,从当前选定的行复制数据,修改该数据,然后将该行添加到表中。

首先,您应该DataTable通过 a绑定您的BindingSource并从中获取选择:

'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)
于 2020-10-28T09:14:13.113 回答