Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I filter a datatable according to certain criteria for a column?

Posted on 2006-07-06
5
Medium Priority
?
363 Views
Last Modified: 2011-09-20
This is what I want. However, I could not understand what was going on...

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20966959.html?query=filter+datatable&topics=327

I have a DataTable. It has many columns and rows. I wish to sort out only those rows that meet a certain criteria.

In this case, this datatable contains, for simplicity, just two columns

MailItem_ID      MailItem_Direction
1      Incoming Mail                
2      Incoming Mail                
10      Outgoing Mail                
11      Outgoing Mail                

I wish to, somehow, filter out "Outgoing Mail" and keep "Incoming Mail".

0
Comment
Question by:M3hcSS
[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
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 17053834
You can do it two ways:

1) Use the DataTable.Select() methos to get an array of DataRows back:

DataRow[] rows = table.Select("MailItem_Direction = 'Incoming Mail'");

2) Create a DataView to get a filtered view of the table

DataView view = new DataView(table);
view.RowFilter = "MailItem_Direction = 'Incoming Mail'";
0
 
LVL 2

Author Comment

by:M3hcSS
ID: 17054151
Let me try the second way.

How do I rebind a datagrid to the dataview?

Right now I have the datagrid bound to the datatable.

0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 1400 total points
ID: 17054180
The same way:

DataView view = new DataView(table);
view.RowFilter = "MailItem_Direction = 'Incoming Mail'";
DataGrid1.DataSource = view;
DataGrid1.DataBind();

You can also apply the filter to the table directly:

table.DefaultView.RowFilter = "MailItem_Direction = 'Incoming Mail'";
DataGrid1.DataSource = table;
DataGrid1.DataBind();
0
 
LVL 7

Expert Comment

by:jj819430
ID: 17054182
DataGrid DG = new DataGrid();
DG.DataSource = myDataView;
DB.DataBind();

0
 
LVL 2

Author Comment

by:M3hcSS
ID: 17054189
                 DataView myDataViewTEST=new DataView(myDataSetMail.Tables["tbl_MailItem"]);
                  myDataViewTEST.RowFilter="MailItem_Direction = 'Incoming Mail'";
                  dgTEST.DataSource=myDataViewTEST;
                  dgTEST.DataBind();
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

610 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