Filtering DataGridView without changing datasource

前端 未结 7 1951
渐次进展
渐次进展 2020-11-22 06:55

I\'m developing user control in C# Visual Studio 2010 - a kind of \"quick find\" textbox for filtering datagridview. It should work for 3 types of datagridview datasources:

7条回答
  •  借酒劲吻你
    2020-11-22 07:39

    I developed a generic statement to apply the filter:

    string rowFilter = string.Format("[{0}] = '{1}'", columnName, filterValue);
    (myDataGridView.DataSource as DataTable).DefaultView.RowFilter = rowFilter;
    

    The square brackets allow for spaces in the column name.

    Additionally, if you want to include multiple values in your filter, you can add the following line for each additional value:

    rowFilter += string.Format(" OR [{0}] = '{1}'", columnName, additionalFilterValue);
    

提交回复
热议问题