adding data to a datatable

B

Billy Cormic

Hello,
I have two datatables A, and B. I am trying to add a row from
table A to table B. So far I have been unsuccessful. Every time I
add a row to table B it replaces the first row in table B rather than
adding an additional row. Can anyone help?

Thanks again,
Billy

'Globals
Dim test123 As New DataTable
Dim test456 As New DataTable


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click

Dim SelectedRowNumber As Integer =
dg_SearchParamOptions.CurrentCell.RowNumber()
Dim SelectedDataRow As DataRow =
test123.Rows(SelectedRowNumber)
Dim CopyOfSelectedDataRow As DataRow

test456 = test123.Clone
'test123.Rows(SelectedRowNumber).Delete()
Try
'test456.ImportRow(SelectedDataRow)

' test456.Rows.InsertAt(SelectedDataRow, LastRow)
' test456.LoadDataRow(SelectedDataRow.ItemArray)
' test456.Rows.Add
' CopyOfSelectedDataRow = test456.NewRow
Dim newRow As DataRow = test456.NewRow()
newRow.ItemArray = SelectedDataRow.ItemArray
Dim LastRow As Integer = test456.Rows.Count
test456.Rows.InsertAt(newRow, test456.Rows.Count)
'test456.Rows.Add(newRow)
test456.AcceptChanges()

DataGrid1.DataSource() = test456
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
 
A

Antoine Cote [MSFT]

Hello Billy,

your solution with ImportRow should work. The thing you need to be careful
about is that DataTable.Clone only copies the strcture of the table, not
its data. If you want to copy both the structure and the data, then you
should use DataTable.Copy. Here's an example of copying a row from one
table to another.

Dim tableA As DataTable = New DataTable
tableA.Columns.Add()
tableA.Rows.Add(New String() {"Blue"})

' Clone the structure of tableA only.
tableB = tableA.Clone()

tableB.ImportRow(tableA.Rows(0))
tableA.Rows.Add(New String() {"Red"})
tableB.ImportRow(tableA.Rows(1))

At the end of this sample, tableB contains 2 rows imported from tableA. The
first rows contains 'Blue' and the second contains 'Red'.

HTH

Antoine
Microsoft Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
B

Billy Cormic

Hello Billy,

your solution with ImportRow should work. The thing you need to be careful
about is that DataTable.Clone only copies the strcture of the table, not
its data. If you want to copy both the structure and the data, then you
should use DataTable.Copy. Here's an example of copying a row from one
table to another.

Dim tableA As DataTable = New DataTable
tableA.Columns.Add()
tableA.Rows.Add(New String() {"Blue"})

' Clone the structure of tableA only.
tableB = tableA.Clone()

tableB.ImportRow(tableA.Rows(0))
tableA.Rows.Add(New String() {"Red"})
tableB.ImportRow(tableA.Rows(1))

At the end of this sample, tableB contains 2 rows imported from tableA. The
first rows contains 'Blue' and the second contains 'Red'.

HTH

Antoine
Microsoft Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------

Thanks Antoine,
It works great now!

Billy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top