Solved

Filtering a DataTable C#

Posted on 2009-04-08
10
541 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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