looping through a dataview

D

David

I am trying to loop through a dataview to collect rows
after they have been sorted, and insert them into a new
table. Here is a code snippet:

DataView dv = (view of table of existing dataset)
DataTable dt = new DataTable();
foreach (DataRowView drv in dv)
{
dt.Rows.Add(drv.Row);
}

This generates the error that the row belongs to another
table. Any suggestions on how to change this to make it
work?

Thanks,

David
 
I

IbrahimMalluf

Hello David

Why don't you just populate a datatable with the sort that you need directly
from the database?

If you just can't do that because of network traffic some other reason that
you are avoiding the obvious then the alternative will not give you the
greatest of performance. You need to create the target table with the
underlying schema then populate each new row column by column. Not an ideal
solution if there are many rows with many columns.

try this:

For Each Myrow As DataRow In MyTable.Rows

daRow = daTable.NewRow

For Icount = 0 To daTable.Columns.Count - 1

daRow.Item(Icount) = Myrow.Item(Icount)

Next

daTable.Rows.Add(daRow)

Next


The above works, but I still recommend just getting a datatable populated
with the right sort directly from the data source.
 

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