create new DataTable

  • Thread starter Thread starter John
  • Start date Start date
J

John

(This is a repost, I got in trouble for not posting it
here first.)

What is the most efficient way to perform a filter on a
DataTable and then have a new DataTable with only the
rows matching the filter?

The only thing I can think of is the following:

1. dataTableNew = dataTableOld.Clone();
2. rows = dataTableOld.Select("some filter");
3. loop through rows and add to dataTableNew

A few step left out, but you get the idea.

Would love to hear your thoughts,
John
 
Hi John,

I would choise this method when the original row is the same as the
receiving row, because than than you can use the
drnew.itemarray = dr(i).itemarray

That cannot when you use the dataview rowfilter althought that is faster
than the select.

You did want some thoughts.
:-)
Cor
 
John:

Use a DataView and set the rowfilter, Then walk through the dataview adding
copies of the rows to your datatable... At the end of my article here
http://www.knowdotnet.com/articles/dataviews1.html You'll see how to do this

You can use the Rowfilter, or Find or FindRows etc

If you have the original data and then you want to add to a second one. you
can use somethign like this:

IEnumerator viewCounter = dvEmployees.GetEnumerator();
DataRowView drv;
while(viewCounter.MoveNext)
{
drv = (DataRowView) viewCounter.Current;
}

except you could use instead DataRow dro = OriginalDataTable.NewRow
dro(0) = 'CurrentRow(0)

You can similarly loop through a DataTable
http://www.knowdotnet.com/articles/dataviewspart2.html using Select. When
you have your array of rows, simply use the same logic inside the array loop

for(int x = 0; x < ArrayCount; x++){

DataRow dro = SortedTable.NewRow();
dro(0) = (DataRow) array[Index](x)(0); //where 0 is the column index

}

Anyway there are multiple ways to do it but they all entail doing a filter,
looping through the results and adding them to new rows. Be careful to use
New Rows and watch those references.. Also, remember that DataTable.Clone
on copies the Schema, not the data, so it will allow you to accurately
create a new row based on it (b/c the schema is the same) but if you filter
it , you'll have no data so it won't work.

HTH,

Bill
 
William,

That's a solid answer. Thank you for your time.

John
-----Original Message-----
John:

Use a DataView and set the rowfilter, Then walk through the dataview adding
copies of the rows to your datatable... At the end of my article here
http://www.knowdotnet.com/articles/dataviews1.html You'll see how to do this

You can use the Rowfilter, or Find or FindRows etc

If you have the original data and then you want to add to a second one. you
can use somethign like this:

IEnumerator viewCounter = dvEmployees.GetEnumerator();
DataRowView drv;
while(viewCounter.MoveNext)
{
drv = (DataRowView) viewCounter.Current;
}

except you could use instead DataRow dro = OriginalDataTable.NewRow
dro(0) = 'CurrentRow(0)

You can similarly loop through a DataTable
http://www.knowdotnet.com/articles/dataviewspart2.html using Select. When
you have your array of rows, simply use the same logic inside the array loop

for(int x = 0; x < ArrayCount; x++){

DataRow dro = SortedTable.NewRow();
dro(0) = (DataRow) array[Index](x)(0); //where 0 is the column index

}

Anyway there are multiple ways to do it but they all entail doing a filter,
looping through the results and adding them to new rows. Be careful to use
New Rows and watch those references.. Also, remember that DataTable.Clone
on copies the Schema, not the data, so it will allow you to accurately
create a new row based on it (b/c the schema is the same) but if you filter
it , you'll have no data so it won't work.

HTH,

Bill
(This is a repost, I got in trouble for not posting it
here first.)

What is the most efficient way to perform a filter on a
DataTable and then have a new DataTable with only the
rows matching the filter?

The only thing I can think of is the following:

1. dataTableNew = dataTableOld.Clone();
2. rows = dataTableOld.Select("some filter");
3. loop through rows and add to dataTableNew

A few step left out, but you get the idea.

Would love to hear your thoughts,
John


.
 
Thanks John ;-)
John said:
William,

That's a solid answer. Thank you for your time.

John
-----Original Message-----
John:

Use a DataView and set the rowfilter, Then walk through the dataview adding
copies of the rows to your datatable... At the end of my article here
http://www.knowdotnet.com/articles/dataviews1.html You'll see how to do this

You can use the Rowfilter, or Find or FindRows etc

If you have the original data and then you want to add to a second one. you
can use somethign like this:

IEnumerator viewCounter = dvEmployees.GetEnumerator();
DataRowView drv;
while(viewCounter.MoveNext)
{
drv = (DataRowView) viewCounter.Current;
}

except you could use instead DataRow dro = OriginalDataTable.NewRow
dro(0) = 'CurrentRow(0)

You can similarly loop through a DataTable
http://www.knowdotnet.com/articles/dataviewspart2.html using Select. When
you have your array of rows, simply use the same logic inside the array loop

for(int x = 0; x < ArrayCount; x++){

DataRow dro = SortedTable.NewRow();
dro(0) = (DataRow) array[Index](x)(0); //where 0 is the column index

}

Anyway there are multiple ways to do it but they all entail doing a filter,
looping through the results and adding them to new rows. Be careful to use
New Rows and watch those references.. Also, remember that DataTable.Clone
on copies the Schema, not the data, so it will allow you to accurately
create a new row based on it (b/c the schema is the same) but if you filter
it , you'll have no data so it won't work.

HTH,

Bill
(This is a repost, I got in trouble for not posting it
here first.)

What is the most efficient way to perform a filter on a
DataTable and then have a new DataTable with only the
rows matching the filter?

The only thing I can think of is the following:

1. dataTableNew = dataTableOld.Clone();
2. rows = dataTableOld.Select("some filter");
3. loop through rows and add to dataTableNew

A few step left out, but you get the idea.

Would love to hear your thoughts,
John


.
 
Back
Top