Solved

Filtering a DataTable C#

Posted on 2009-04-08
10
543 Views
Last Modified: 2012-05-06
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
Comment
Question by:csharp_learner
[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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24103973
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
 
LVL 2

Expert Comment

by:AlvinLim84
ID: 24103992
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
 

Author Comment

by:csharp_learner
ID: 24104249
So how can i get the value in column 5 after the
myDataTable.DefaultView.RowFilter = "ID = 2" statement?
0
Independent Software Vendors: 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!

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24104263
DataRow[]Rows = DataTable.Select("ID = 2","");
Rows[0][5] would give the value in column five in row 0.
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24104275
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
 

Author Comment

by:csharp_learner
ID: 24104329
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
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24104351
try:
DataSet1.Tables("TableName").Columns("ID").Expression = "2
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24104356
Change DataTable to your DataTable object, don't just use the class name:
DataRow[] Rows = myDataTable.Select("ID = 2","");
0
 

Author Comment

by:csharp_learner
ID: 24104643
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
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 350 total points
ID: 24104867
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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