DataGridView.DefaultView清除过滤器

时间:2016-04-12 10:01:20

标签: c# datagridview

如何自动或通过按钮清除应用的过滤器?

我使用以下代码从表单2中过滤DataGridView(表单1)中的frPlanMain

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plan_de_lucru_1._0
{
    public partial class SearchWindow : Form
    {
        public frPlanMain refTofrPlanMain;

        public SearchWindow(frPlanMain f) //<<Edit made here 
        {
            refTofrPlanMain = f;
            InitializeComponent();
        }

        private void SearchButtonW_Click(object sender, EventArgs e)
        {
            {
                (refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
                for (int i = refTofrPlanMain.dGVPlan.Rows.Count - 1; i >= 0; i--)
                {
                    DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
                    if (item.Visible)
                    {
                        refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
                        break;                      
                    }
                //
                }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

要清除DataView上的过滤器,请将其RowFilter属性设置为空字符串

 yourDataView.DefaultView.RowFilter = ""

答案 1 :(得分:1)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plan_de_lucru_1._0
{
    public partial class SearchWindow : Form
    {
        public frPlanMain refTofrPlanMain;

        public SearchWindow(frPlanMain f) //<<Edit made here 
        {
            refTofrPlanMain = f;
            InitializeComponent();
        }

        private void SearchButtonW_Click(object sender, EventArgs e)
        {
            (refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
            for (int i = refTofrPlanMain.dGVPlan.Rows.Count - 1; i >= 0; i--)
            {
                DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
                if (item.Visible)
                {
                    refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
                    break;                      
                }
            }
        }
        private void clearFilter_onClick(object sender, EventArgs e){
            (refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = null;            
        }
    }
}

见这里:How to reset bindingsource filter to nothing