472,995 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 software developers and data experts.

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 5657
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
by: Brian Wotherspoon | last post by:
I have a table with data that is refreshed regularly but I still need to store the old data. I have created a seperate table with a foreign key to the table and the date on which it was replaced. ...
by: Mantorok | last post by:
Hi all I have a DataTable containing around 25000 rows. For each row I want to query the entire DT to find out if potentially duplicate items exist, problem is - this is reeeeeeaaaaal slow. ...
by: Ben R. | last post by:
Hi guys, I've got a DB table of timecards with these fields in the table: ID (Int) UserID (Int) DateWorked (DateTime) HoursWorkedOnThatDate (Double) I'd like to display a grid, with...
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio...
by: sql_er | last post by:
Guys, I have an XML file which is 233MB in size. It was created by loading 6 tables from an sql server database into a dataset object and then writing out the contents from this dataset into an...
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.