Solved

Select Query on a DataTable to obtain a new DataTable

Posted on 2008-10-08
4
553 Views
Last Modified: 2012-05-05
Hi,

I'd like to execute a select query on a DataTable, and obtain as a result a new DataTable only populating with the columns that I've defined in my query.

For example, I've got a DataTable with 3 columns: DataSetId (Long Integer), X (double), Y (double).
And I want to execute the query "SELECT X,Y FROM MyTable WHERE DataSetId=2", to obtain a new datatable with only two columns X and Y, and the selected rows.

Any ideas?

Thanks!
0
Comment
Question by:JulienVan
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 450 total points
ID: 22666914
You can just use DataView:
DataView dv = new DataView(dataTable, "DataSetId = 2", string.Empty, DataViewRowState.CurrentRows);
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 total points
ID: 22666954
use the dataview as suggested by philip, and hide the datasetid column if you are using a datagridview.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22666962
if you want to change the dataview's filter at any time you can do something like:

dv.RowFilter = "DataSetId = " + someId.ToString();
0
 
LVL 2

Author Comment

by:JulienVan
ID: 22667078
Thank you for your quick responses, using a dataview is a great idea.

I use myDataView.ToTable(false, new string[] { "X", "Y" }); to obtain my new datatable.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now