How to make the list of Items displayed in the dataGridView to be (filtered?)

浪子不回头ぞ 提交于 2019-12-25 09:24:50

问题


How to do this?

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 uses a list of items as its datasource similar to this:

dgv.DataSource = _myItemList;

Each item of _myItemListhas 2 properties (id, description)

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

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)

The auto stuff happens with this code

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

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

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.


回答1:


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

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 +"%'";
        }

Result;

Hope helps,



来源:https://stackoverflow.com/questions/40388502/how-to-make-the-list-of-items-displayed-in-the-datagridview-to-be-filtered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!