Solved

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

Posted on 2006-07-06
5
350 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 350 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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