简体   繁体   English

如何使用 TextBox 过滤 DataGridView

[英]How to filter DataGridView using TextBox

I don't have much experience yet.我还没有太多经验。 I'm trying to filter rows which contain the input text.我正在尝试过滤包含输入文本的行。 In order.. I use MailKit to receive incoming messages and put data from them in Grid为了.. 我使用 MailKit 接收传入的消息并将来自它们的数据放入 Grid

public void ShowMessages(ImapClient client)
{
    var inbox = client.Inbox;
    inbox.Open(FolderAccess.ReadOnly);
    var query = SearchQuery.All;                                                                             
    var uids = inbox.Search(query);
    var items = inbox.Fetch(uids, MessageSummaryItems.Full | MessageSummaryItems.BodyStructure).Reverse();
    foreach (var item in items)
    {
       ...                                 
       dataGridMessages.Rows.Add(read, item.Envelope.Subject, item.Envelope.From, item.Envelope.Date, attach, item.Size, false, item.UniqueId);
    }
}

Then I try to filter the rows然后我尝试过滤行

private void searchTxb_TextChanged(object sender, EventArgs e)
{
    BindingSource bs = new BindingSource();
    bs.DataSource = dataGridMessages.DataSource;
    bs.Filter = string.Format("Subject LIKE '%{0}%'", searchTxb.Text);
    dataGridMessages.DataSource = bs;
}

but after typing the text the grid is empty, finds nothing.但在输入文本后,网格为空,什么也没找到。 Please tell me what I'm doing wrong?请告诉我我做错了什么?

Your problem is caused by the fact that you fill the grid one row at time adding directly a row.您的问题是由于您一次填充网格一行而直接添加一行。 In this way, the DataSource property is not set to anything and if you want to filter you need to loop over the rows one by one and remove the unwanted rows.这样,DataSource 属性未设置为任何内容,如果要过滤,则需要逐一循环遍历行并删除不需要的行。

In alternative you could use the BindingList<T> where T is a custom class that you define in your code with only the properties that you want to be displayed in the grid.或者,您可以使用BindingList<T>其中 T 是您在代码中定义的自定义 class ,其中仅包含您希望在网格中显示的属性。 With this class defined you can create the instances to add to the List used in the BindingList constructor定义了这个 class 后,您可以创建实例以添加到 BindingList 构造函数中使用的列表

So suppose to have a class like this所以假设有一个像这样的 class

private class MailItem
{
    public string Subject {get;set;}
    public string From {get;set;}
    public DateTime DateSent {get;set;}
}

and a global variable inside your form of type以及类型形式中的全局变量

List<MailItem> rows = new List<MailItem>();

For simplicity I have added just some properties, but you can add the others easily.为简单起见,我只添加了一些属性,但您可以轻松添加其他属性。
Now when you need to fill the grid use this现在,当您需要填充网格时,请使用它

...
rows.Clear();
var items = inbox.Fetch(uids, MessageSummaryItems.Full | MessageSummaryItems.BodyStructure).Reverse()
foreach(var item in items)
{
    rows.Add(new MailItem {
       Subject = x.Envelope.Subject,
       From = x.Envelope.From,
       DateSent = x.Envelope.Date     
    };
}

Now you can create the BindingList and set it as the DataSource of the grid现在您可以创建 BindingList 并将其设置为网格的 DataSource

BindingList<MailItem> bs = new BindingList<MailItem>(rows);
dataGridMessages.DataSource = bs;

The last step is the filtering code.最后一步是过滤代码。 Here you need to extract the rows you want to use and apply the filter在这里,您需要提取要使用的行并应用过滤器

BindingList<MailItem> currentFilter = new BindingList<MailItem>(rows);
dataGridMessages.DataSource = currentFilter.Where(x => x.Subject.Contains(searchTxb.Text)).ToList();

Note that you need to preserve the original set of rows retrieved by the call to Fetch in such a way that you can apply a different filter while your user changes the textbox content.请注意,您需要保留通过调用 Fetch 检索到的原始行集,以便您可以在用户更改文本框内容时应用不同的过滤器。 So, the list of MailItem should be kept global for your form因此,MailItem 列表应该为您的表单保持全局

In alternative you can recall Fetch again, but it seems that this method is pretty slow even for just 100 elements to retrieve.或者,您可以再次调用 Fetch,但即使仅检索 100 个元素,此方法似乎也很慢。 A better model (IMHO) is to implement a button and call the filter on that button click.更好的 model (恕我直言)是实现一个按钮并在该按钮单击时调用过滤器。

(BindingList requires using System.ComponentModel ) (BindingList 需要使用 System.ComponentModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM