Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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