Solved

Filtering a DataTable C#

Posted on 2009-04-08
10
540 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

937 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

Need Help in Real-Time?

Connect with top rated Experts

3 Experts available now in Live!

Get 1:1 Help Now