在C#中,我们使用DevExpress控件会用到Gridview表格,有时候我们需要按条件来过滤GridControl控件中的表格数据,只需要给Textbox文本加入EditValueChanging这个值改变事件即可,代码如下所示:
//EditValueChanging这个值改变事件 private void filter_column_text(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) { this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never; this.gridView1.ActiveFilterCriteria = BulidFilterCriteria(); this.gridView1.RefreshData(); } //过滤方法,filter_text.Text是需要过滤的文本内容 private GroupOperator BulidFilterCriteria() { CriteriaOperatorCollection filterCollection = new CriteriaOperatorCollection(); //四列中都参与数据过滤 filterCollection.Add(CriteriaOperator.Parse(string.Format("MTNO LIKE '%{0}%'", filter_text.Text))); filterCollection.Add(CriteriaOperator.Parse(string.Format("MTNAME LIKE '%{0}%'", filter_text.Text))); filterCollection.Add(CriteriaOperator.Parse(string.Format("PYCODE LIKE '%{0}%'", filter_text.Text))); filterCollection.Add(CriteriaOperator.Parse(string.Format("WBCODE LIKE '%{0}%'", filter_text.Text))); return new GroupOperator(GroupOperatorType.Or, filterCollection); }
上面有一个地方需要注意GroupOperatorType,它是一个enum枚举类型,有两个选项Or与And。
1:因为上方的案例中只有一个Textbox文本输入框作为条件,所以要想四列中有一条符合条件就要展示,就应该用GroupOperatorType.Or,它表示“或”结果,有一列满足条件就会展示。
2:假如我们有多个Textbox文本框来输入条件filter过滤,所有的条件同时满足才让它展示,则需要用GroupOperatorType.And,它表示“与”结果,意思就是要同时满足多个条件才会展示结果。
例如以下这种多条件过滤的情况: