Solved

Select Query on a DataTable to obtain a new DataTable

Posted on 2008-10-08
4
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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