Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 42
how to convert MM/dd/YYYY hh:mm:ss AM to YYYY-MM-dd datetime format by linq? 4 50
Cant save 3D 4 19
HttpPostedFile 1 26
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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