[Webinar] Streamline your web hosting managementRegister Today

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

Filtering a DataTable C#

Hi,
I have a DataTable where i would like to filter out a datarow.

Lets say my DataTable Column looks something like this:
ID , Name , Description , Model , Type
Can I do something like "Get Data from coulmn 5 where ID is 2"?
0
csharp_learner
Asked:
csharp_learner
  • 3
  • 3
  • 2
  • +2
1 Solution
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
You can use the DataTable.Select method or DataTable.Compute method
to perform queries against a DataTable.

Decimal val = dt.Compute("Sum(quantity)","company='IBM'");

http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx
http://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx
0
 
AlvinLim84Commented:
You can use DefaultView.RowFilter also

http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview(VS.71).aspx
myDataTable.DefaultView.RowFilter = "ID = 2"

Open in new window

0
 
csharp_learnerAuthor Commented:
So how can i get the value in column 5 after the
myDataTable.DefaultView.RowFilter = "ID = 2" statement?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Gautham JanardhanCommented:
DataRow[]Rows = DataTable.Select("ID = 2","");
Rows[0][5] would give the value in column five in row 0.
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
using DataColumn Expression you can do something like
DataSet1.Tables("Orders").Columns("OrderCount").Expression = "Count(OrderID)"

look at: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
0
 
csharp_learnerAuthor Commented:
When i tried entering
DataRow[]Rows = DataTable.Select("ID = 2","");
the . Select is not in the list...the list consist of only Equals and ReferenceEquals
0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
try:
DataSet1.Tables("TableName").Columns("ID").Expression = "2
0
 
philipjonathanCommented:
Change DataTable to your DataTable object, don't just use the class name:
DataRow[] Rows = myDataTable.Select("ID = 2","");
0
 
csharp_learnerAuthor Commented:
i tried
DataRow[] Rows = ordt.Select("ID = 2", "");
comboBox1.Text = ordt.Rows[0][5].ToString();

but again the same error came out" there is now row at position 0"
0
 
Gautham JanardhanCommented:
if ur datatable doesnt contain an id with value = 2 the rows returned will be 0
DataRow[] Rows = ordt.Select("ID = 2", "");
if(Rows.Lenght > 0)
{
comboBox1.Text = ordt.Rows[0][5].ToString();
}

Open in new window

0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

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