Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I wrote following method filter for dataGrid which binded to List<>
public void FilterFromSubject(string mailSubject)
      {
          List<unreademails> list = (List<unreademails>)dgvUnreadMails.DataSource;
          //List<string> myUnreadMailList;
          try
          {
              List<unreademails> filteredList = (List<unreademails>)(from ci in list
                                                                     where ci.Subject.ToLower().Contains(mailSubject)

                                                                     select ci).ToList<unreademails>();
              dgvUnreadMails.DataSource = filteredList;
              btnCancel.Enabled = true;
          }
          catch (System.Exception)
          {

              MessageBox.Show("Import E-mails first", "Empty Table", MessageBoxButtons.OK, MessageBoxIcon.Information);
              btnSave.Enabled = false;
          }
      }</unreademails></unreademails></unreademails></string></unreademails></unreademails>



Now I am having data grid binded with Dynamic DataTable as follows
private object GetDynamicDataTable(List<unreademails> emailList, List<string> words)
      {
          DataTable dtList = new DataTable();
          dtList.Columns.Add("Sender Name");
          dtList.Columns.Add("Sender Address");
          dtList.Columns.Add("Date Time");
          dtList.Columns.Add("Subject");
          dtList.Columns.Add("Body Content");
          dtList.Columns.Add("Attachments");
          dtList.Columns.Add("EntryID");

          foreach (string dynamic in words)
          {
              dtList.Columns.Add(dynamic);
              cmbWords.Items.Add(dynamic);
          }

          foreach (UnreadEmails mail in emailList)
          {
              DataRow dr = dtList.NewRow();
              dr["Sender Name"] = mail.SenderName;
              dr["Sender Address"] = mail.SenderAddress;
              dr["Date Time"] = mail.RecivedTime;
              dr["Body Content"] = mail.BodyContent;
              dr["Subject"] = mail.Subject;
              dr["EntryID"] = mail.EntryID;
              foreach (string item in words)
              {
                  object count = GetWordCount(mail.BodyContent.ToString(), item);
                  dr[item] = count;
              }

              dtList.Rows.Add(dr);
          }
          return dtList;
      }</string></unreademails>


Please tell me how can I write filtering method for data grid which is binded to DataTable.
Posted
Updated 19-Aug-10 5:42am
v2

1 solution

I believe you can do this with a BindingSource object as the intermediate to your DataGridView and your DataTable. You can set the Filter property of the BindingSource to whatever you need.

First, set the BindingSource as the DataSource to your DataGridView and set up the filter:

MIDL
dgvUnreadMails.DataSource = aBindingSource;
aBindingSource.Filter = "something";


later on, after your DataTable (dtList) has been loaded, you can set it equal to the DataSource of the BindingSource.

MIDL
aBindingSource.DataSource = dtList;
 
Share this answer
 
Comments
Kasunmit 19-Aug-10 23:30pm    
thanx for ur kind help can you pls specify type of aBindingSource variable .. thanx
jasonHall 20-Aug-10 9:56am    
Here is the URL for the MSDN page on BindingSource:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx
jasonHall 20-Aug-10 10:00am    
This link was on that page as well. It gives you another example of how to filter using the BindingSource!

http://msdn.microsoft.com/en-us/library/ya3sah92.aspx
Kasunmit 22-Aug-10 0:33am    
thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900