Solved

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

Posted on 2006-07-06
5
339 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
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:dstanley9
Comment Utility
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
Comment Utility
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 350 total points
Comment Utility
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
Comment Utility
DataGrid DG = new DataGrid();
DG.DataSource = myDataView;
DB.DataBind();

0
 
LVL 2

Author Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

16 Experts available now in Live!

Get 1:1 Help Now