搜索数据网格问题 [英] Search in data grids problem

查看:73
本文介绍了搜索数据网格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为绑定到List的dataGrid编写了以下方法过滤器

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>




现在,我将数据网格与Dynamic DataTable绑定,如下所示




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>



请告诉我如何为绑定到DataTable的数据网格编写过滤方法.



Please tell me how can I write filtering method for data grid which is binded to DataTable.

推荐答案

我相信您可以使用BindingSource对象作为中间对象来完成此操作DataGridView和您的DataTable.您可以将BindingSource的Filter属性设置为所需的任何内容.

首先,将BindingSource设置为DataGridView的DataSource并设置过滤器:

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:

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



稍后,在加载DataTable(dtList)之后,可以将其设置为与BindingSource的DataSource相等.



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

aBindingSource.DataSource = dtList;


这篇关于搜索数据网格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆