470,607 Members | 2,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,607 developers. It's quick & easy.

The most efficient way to build a new data table based DataTable.Select result


I am working with strongly typed datatables.
What is the most efficient way to build a new DataTAble based on the result
of DataTable.Select?
At this point I use a foreach loop to do the work manually. I am looking for
an automated way.

Thank you,
Nov 3 '06 #1
1 5457
Hi Max,

I am sorry that I don't think there's an automated way to build a new
DataTable based on the result of calling DataTable.Select method.

The result of calling DataTable.Select method is an array of DataRow. So
the question is how to fill a DataTable with an array of DataRow in an
automated way.

At present, we have two ways to fill data into a DataTable. One way is to
call SqlDataAdapter.Fill or TableAdapter.Fill method to fill data from
database into the DataTable. The other way is to call DataTable.Rows.Add
method to add rows into the DataTable manually.

So we have to do fill a DataTable with an array of DataRow manually.

I recommend you to add a public method in the DataTable class to construct
a new datatable with an array of DataRow. To do this, open the DataSet in
your project in the designer and double-click on the DataTable to open the
code file of the DataSet. Then you could add a public method in the
DataTable class.

The following is a sample.

using System.Data;

partial class DataSet1
partial class DataTable1
public DataTable1 SelectToATable(string filterExpression)
DataTable1 table = this.Clone() as DataTable1;
DataRow[] selectedRows = this.Select(filterExpression);

DataRow newrow = null;

for (int i = 0; i < selectedRows.Length; i++)
newrow = table.NewRow();
newrow.ItemArray = selectedRows[i].ItemArray;
return table;

Then you could call the SelectToATable method in the application and
needn't write any extra code to construct a new DataTable.

Hope this helps.
If you have any concerns, please feel free to let me know.

Linda Liu
Microsoft Online Community Support

Get notification to my posts through email? Please refer to

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at

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

Nov 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by pmud | last post: by
9 posts views Thread by Brad | last post: by
3 posts views Thread by Brian Wotherspoon | last post: by
5 posts views Thread by Mantorok | last post: by
9 posts views Thread by Ben R. | last post: by
5 posts views Thread by sql_er | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.