?
Solved

Select Query on a DataTable to obtain a new DataTable

Posted on 2008-10-08
4
Medium Priority
?
574 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 1350 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 150 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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