Solved

Filtering a DataTable C#

Posted on 2009-04-08
10
544 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
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!

 
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

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!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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