如何使显示在dataGridView中的项目列表成为(过滤?) [英] How to make the list of Items displayed in the dataGridView to be (filtered?)

查看:104
本文介绍了如何使显示在dataGridView中的项目列表成为(过滤?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何执行此操作?

从用户的角度来看,C#Winforms随着用户在搜索文本框中键入内容,我希望dataGridView中显示的项目列表是(已过滤?)

From the users perspective, C# Winforms, as user types into a search textbox, I want the list of Items displayed in the dataGridView to be (filtered?)

这是当前正在发生的事情:

Here’s what is currently happening:

dataGridView使用项目列表作为其数据源与此类似:

dataGridView uses a list of items as its datasource similar to this:

dgv.DataSource = _myItemList;

dgv.DataSource = _myItemList;

_myItemList的每个项目都具有2个属性(id,描述)

Each item of _myItemListhas 2 properties (id, description)

在dgv下方有一个文本框,用户可以在其中搜索列表中的项目

Below the dgv there is a textbox in which user can search for items in the list

当前,当用户在文本框中键入内容时,下面会出现一个已过滤的_myItemList(与该已过滤列表的显示方式相同,这就是我想对dgv进行的操作)

Currently, when user types in the textbox , a filtered _myItemListappears below (the same way this filtered list appears is what I want to happen with the dgv)

代码

foreach(_myItemList中的可变项)
{
string word = item.Description;
textBoxCollection.Add(word);
}

foreach (var item in _myItemList) { string word = item.Description; textBoxCollection.Add(word); }

        textBoxSearchEmployeeList.AutoCompleteMode = AutoCompleteMode.Suggest;
        textBoxSearchEmployeeList.AutoCompleteSource = AutoCompleteSource.CustomSource;
        textBoxSearchEmployeeList.AutoCompleteCustomSource = textBoxCollection;

例如,如果用户在文本框中键入 Vac,则_myItemList中所有以 Vac开头的字符串会出现。然后,随着用户继续键入,dgv中的项目将减少。这样,当用户在文本框中输入假期时,dgv仅显示一行假期。

Eg, if user types "Vac" into the textbox, all strings in _myItemList starting with "Vac" will appear. Then as user continues to type the items in dgv will decrease. So that when user types "Vacation" into the textbox, then the dgv displays only one row, "Vacation".

如果不清楚我要做什么,请让我知道。

If it’s unclear what I’m trying to do, please let me know.

推荐答案

如果我听错了,您正在寻找,

If I didn't understand wrong, you are looking for this,

我用行创建了简单的 DataTable 。 (您可以在gif中看到)并且我有一个文本框要过滤。在这里,

I have created simple DataTable with rows. (you can see at gif) And I have a textbox to filter. Here,

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataView dv = (dataGridView1.DataSource as DataTable).DefaultView;
            dv.RowFilter = "Name Like '"+ textBox1.Text +"%'";
        }

结果;

希望有帮助,

这篇关于如何使显示在dataGridView中的项目列表成为(过滤?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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