禁止或取消筛选DataGridView编辑 [英] Suppress or Cancel Filter on DataGridView edit

查看:398
本文介绍了禁止或取消筛选DataGridView编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DataGridView允许编辑(ReadOnly = false)。 DataGridView还可能有一个或多个与其数据源关联的过滤器。例如:

 (myDataGridView.DataSource as DataTable).DefaultView.RowFilter =[myColumn] ='value'; 

如果应用了过滤器,并且用户编辑了myColumn中的字段,行立即消失 ,因为它不再符合过滤器的标准。有没有办法抑制或取消这个动作?理想情况下,我希望用户刷新网格,以便随时重新应用该过滤器。

解决方案

由King King所说的不是很好,因为它也会显示与该列具有相似值的所有其他行。



您可以选择避免使用 DataTable.DefaultView.RowFilter 的自动过滤机制,而是执行循环DataGridView的所有行,并检查您的过滤条件以设置每行可见属性。

  void applyFilter()
{
foreach(Grid1.Rows中的DataGridViewRow行)
{
string columnA = row.Cells [ColumnA]。
string columnB = row.Cells [ColumnB]。
row.Visible =(columnA ==valueA&& columnB ==valueB);
}
}

您还可以使用以下内容: columnA.IndexOf(valueA,StringComparison.OrdinalIgnoreCase)> -1 使用'包含'标准进行搜索(例如 RowFilter ='%valueA%')。 p>

I have DataGridView that allows editing (ReadOnly = false). The DataGridView may also have one or more filters associated with its datasource. For example:

(myDataGridView.DataSource as DataTable).DefaultView.RowFilter = "[myColumn] = 'value'";

If the filter is applied, and the user edits the field in myColumn, the row immediately "disappears", as it no longer fulfills the filter's criteria. Is there a way to suppress or cancel this action? Ideally, I want the user to "refresh" the grid so that the filter is re-applied at will.

解决方案

The mehod stated by 'King King' is not so good because it causes to show also all other rows which have similar values for that column.

You have an option to avoid using auto filter mechanism of DataTable.DefaultView.RowFilter and instead perform a loop through all rows of your DataGridView and check your filteringcriteria to set each row Visible property.

    void applyFilter()
    {
        foreach (DataGridViewRow row in grid1.Rows)
        {
            string columnA = row.Cells["ColumnA"].Value as string;
            string columnB = row.Cells["ColumnB"].Value as string;
            row.Visible = (columnA == "valueA" && columnB == "valueB");
        }
    }

you may also use something like as: columnA.IndexOf("valueA", StringComparison.OrdinalIgnoreCase) > -1 to search with 'Contains' criteria (like as RowFilter = '%valueA%').

这篇关于禁止或取消筛选DataGridView编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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