By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,882 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,882 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi,

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,
Max
Nov 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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;
table.Rows.Add(newrow);
}
table.AcceptChanges();
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.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

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.