Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

Select Query on a DataTable to obtain a new DataTable

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
JulienVan
Asked:
JulienVan
  • 2
2 Solutions
 
philipjonathanCommented:
You can just use DataView:
DataView dv = new DataView(dataTable, "DataSetId = 2", string.Empty, DataViewRowState.CurrentRows);
0
 
Jaime OlivaresCommented:
use the dataview as suggested by philip, and hide the datasetid column if you are using a datagridview.
0
 
Jaime OlivaresCommented:
if you want to change the dataview's filter at any time you can do something like:

dv.RowFilter = "DataSetId = " + someId.ToString();
0
 
JulienVanAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now