Working with datatable.

  • Thread starter Thread starter Jensen bredal
  • Start date Start date
J

Jensen bredal

I have a DataTabel object filled with table data from a database.

The table contains rows that have the same data in a particular column.


How can i make a new copy of the datatable removing the repeating rows?

Many thanks in advance
JB
 
Hi Jensen,

Create a DataView for the original DataTable sorted by that column. Create a
new DataTable you'll be inserting rows to. Start from the first DataView's
row, adding a new row to the destination table with all the data from the
source row. Then skip all rows having the same value in the column. Once the
value in the column changes, add this row and again skip further rows having
the same value. Do so until you've processed all rows in the view.

This is a off-the-top-of-my-head solution, I can't remember whether
DataTable's Select method supports the DISTINCT feature.
 
great ,

How could this be done in an sql statement if i could directly acces the
source table?



Dmitriy Lapshin said:
Hi Jensen,

Create a DataView for the original DataTable sorted by that column. Create
a new DataTable you'll be inserting rows to. Start from the first
DataView's row, adding a new row to the destination table with all the
data from the source row. Then skip all rows having the same value in the
column. Once the value in the column changes, add this row and again skip
further rows having the same value. Do so until you've processed all rows
in the view.

This is a off-the-top-of-my-head solution, I can't remember whether
DataTable's Select method supports the DISTINCT feature.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Jensen bredal said:
I have a DataTabel object filled with table data from a database.

The table contains rows that have the same data in a particular column.


How can i make a new copy of the datatable removing the repeating rows?

Many thanks in advance
JB
 
Jensen,
How could this be done in an sql statement if i could directly acces the
source table?

Almost exactly as Dimitry says using the Select Distinct.

The Net system.data namespace does not support a Distinct, the method that
Mehdi shows and Dimitry suggest in this thread is in my opinion as well the
best method for is. (Be aware that the showed method does only move one
field, you can of course do more fields and than count)

I hope this helps

Cor
 
Back
Top