Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datarow() to Datatable

Status
Not open for further replies.

l310564

Programmer
Jun 18, 2006
50
GB
Hi

I have an array of datarows that i wish to put into a new table, i'm completly stuck so i really hope some one can help.

The code looks something like this

dim dstable as new data.datatable
dim dr as datarow()

dr = <array of datarows>

dstable.rows.add(dr)

obviously this doesn't work but you get the picture.

Cheers,

Hugh


If knowlege can create problems it is not through ignorance that we will solve them.

Isaac Asimov
 
Maybe:

Code:
dim dstable as new data.datatable
dim dr as datarow()

dr = <array of datarows>

For each myrow as datatrow in dr
    dstable.rows.add(myrow)
Next

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
No doesn't work. it gives the error "This row already belongs to anouther table"

Hugh

If knowlege can create problems it is not through ignorance that we will solve them.

Isaac Asimov
 
Can you post the code you have??? Not the "similar" code you posted the first time but the actual code you are trying to use.

Reason I ask is because I use the below quite often and have no difficulties. I know I am probably not doing this the best way, but for the time being I know it works and since my data consists of small sets, it ain't so bad.

Code:
Dim dt As DataTable = SomeDataset.Tables("TableName")
Dim drSelectedRows As DataRow = dt.Select() ' This selects ALL rows from the specified table, Use dt.Select("FieldName = '" & criteria & "'" to search for certain records
Dim dtDestination As DataTable = SomeDataset.Tables("TableName") ' This is the table you want to put the rows in.  If the table is a "new" table, you also need to add columns to the table first with dtDestination.Columns.Add("ColumnName") - one time for each column, etc

For Each dr As DataRow In drSelectedRows
    dtDestination.Rows.Add(dr)
Next

The above it typed from memory and not tested as written. You may have to tweak it a bit, but the gist is there.

I think you are getting the error you are getting because the rows you are trying to write to a new table DO still belong to the original table. In the above example, you are creating a COPY of the datarows into an array of rows, then using this array as the source for the add and not the array of datarows in the original table.

Just my opinion - all comments/suggestions welcomed.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Code:
Dim dtDates As New DataTable
'Clone the orginal table to get it's structure
dtDates = ds.Tables(2).Clone  ''Use whatever dataset or datatable you need here
Dim i As Integer
For i = 0 To drDates.Length - 1
   dtDates.ImportRow(drDates(i))
Next

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top