Winform系列——好⽤的DataGridview过滤控件(表格的⾼级搜索功能)
上⼀篇中主要介绍了DataGridview的表格多级折叠功能。这章主要介绍下最近封装的另⼀个DataGridview表格⾼级过滤的功能。此功能也是参照codeproject上⾯的源码改写的,代码可能有源码的内容,也有本⼈改写过的,所以看上去可能有点乱。废话不多说,上图:
1、⼀般的DataGridview效果:
2、增加了列上⾯右键效果:
3、升序和降序就没什么说的了,看看点击过滤的效果吧:
4、取消某⼀个字段过滤的⽅式有两种:
5、取消所有字段的过滤:
⼤致效果就如上了。代码有点乱,如果有时间我会整理下,有兴趣的可以看看。
1、向外暴露的使⽤类:这个类主要功能是通过构造函数 public DgvFilterManager(DataGridView dataGridView, bool autoCreateFilters){}将DataGridview对象传进来,然后再给
DataGridview增加事件和⽅法实现的。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.ComponentModel;
using Ewin.Client.Frame;
using Ewin.Client.Frame.Controls;
using System.Collections;
using System.Reflection;
namespace Ewin.Client.Frame.UcGrid
{
public class DgvFilterManager
{
#region PRIVATE FIELDS
private DgvBaseFilterHost mFilterHost;      // The host UserControl to popup
private DataGridView mDataGridView;        // The DataGridView to which apply filtering
private DataView mBoundDataView;            // The DataView to which the DataGridView is bound
BindingSource mBindingSource;              // The BindingSource, if any, to which the DataGridView is bound private string mBaseFilter = "";            // Developer provided filter expression
private int mCurrentColumnIndex = -1;      // Column Index of currently visibile filter
private List<DgvBaseColumnFilter> mColumnFilterList;    // List of ColumnFilter objects
private bool mAutoCreateFilters = true;
private bool mFilterIsActive = false;
ContextMenuStrip oMenuStrip;
#endregion
#region EVENTS
///<summary>
/// Occurs when a <i>column filter</i> instance for a column is about to be automatically created.
///</summary>
///<remarks>
/// Using this event you can set the <see cref="ColumnFilterEventArgs.ColumnFilter"/>
/// property to force the <see cref="DgvBaseColumnFilter"/> specialization to use for the
/// column.
/// This event is raised only if <see cref="DgvFilterManager.AutoCreateFilters"/> is true.
///</remarks>
public event ColumnFilterEventHandler ColumnFilterAdding;
///<summary>
/// Occurs when a <i>column filter</i> instance is created.
/// This event is raised only if <see cref="DgvFilterManager.AutoCreateFilters"/> is true.
/
//</summary>
public event ColumnFilterEventHandler ColumnFilterAdded;
///<summary>
/// Occurs when the popup is about to be shown
///</summary>
///<remarks>
/// Use this event to customize the popup position. Set the Handled property of the event argument to <c>true</c>.
///</remarks>
public event ColumnFilterEventHandler PopupShowing;
#endregion
#region CONSTRUCTORS
///<summary>
/// Initializes a new instance of the <see cref="DgvFilterManager"/> class.
///</summary>
public DgvFilterManager() { }
///<summary>
/// Initializes a new instance of the <see cref="DgvFilterManager"/> class.
///</summary>
///<param name="dataGridView">The <b>DataGridView</b> to which attach filtering capabilities</param>
///<param name="autoCreateFilters">if set to <c>true</c> automatically creates a <i>column filter</i> for each column</param> public DgvFilterManager(DataGridView dataGridView, bool autoCreateFilters)
{
this.mAutoCreateFilters = autoCreateFilters;
this.DataGridView = dataGridView;
}
///<summary>
/// Initializes a new instance of the <see cref="DgvFilterManager"/> class.
///</summary>
///<param name="dataGridView">The <b>DataGridView</b> to which attach filtering capabilities.</param>
public DgvFilterManager(DataGridView dataGridView) : this(dataGridView, true) { }
#endregion
#region PROPERTIES
///<summary>
/// Gets or sets a value indicating whether the manager must create <i>column filters</i>.
///</summary>
///<value><c>true</c> by default.</value>
public bool AutoCreateFilters
{
get { return mAutoCreateFilters; }
set { mAutoCreateFilters = value; }
}
menustrip和toolstrip
///<summary>
/// Gets and sets the <i>filter host</i> to use.
/
//</summary>
///<remarks>
/// The default <i>filter host</i> is an instance of <see cref="DgvFilterHost"/>
///</remarks>
public DgvBaseFilterHost FilterHost
{
get
{
if (mFilterHost == null)
{
// If not provided, use the default FilterHost
FilterHost = new DgvFilterHost();
//FilterHost.AllowDrop = true;
//FilterHost.Popup.MouseDown += FilterHost_MouseDown;
}
return mFilterHost;
}
set
{
mFilterHost = value;
// initialize FilterManager to this object
mFilterHost.FilterManager = this;
mFilterHost.Popup.Closed += new ToolStripDropDownClosedEventHandler(Popup_Closed);
}
}
void oForm_DragEnter(object sender, DragEventArgs e)
{
//当Button被拖拽到WinForm上时候,⿏标效果出现
if ((e.Data.GetDataPresent(typeof(DgvFilterHost))))
{
e.Effect = DragDropEffects.Copy;
}
}
void oForm_DragDrop(object sender, DragEventArgs e)
{
//拖放完毕之后,⾃动⽣成新控件
//Button btn = new Button();
//btn.Size = Button1.Size;
//btn.Location = this.PointToClient(new Point(e.X, e.Y));
////⽤这个⽅法计算出客户端容器界⾯的X,Y坐标。否则直接使⽤X,Y是屏幕坐标
//this.Controls.Add(btn);
//btn.Text = "按钮" + count.ToString();
//count = count + 1;
}
void FilterHost_MouseDown(object sender, MouseEventArgs e)
{
//左键的话,标志位为true(表⽰拖拽开始)
if ((e.Button == System.Windows.Forms.MouseButtons.Left))
{
FilterHost.DoDragDrop(FilterHost, DragDropEffects.Copy | DragDropEffects.Move);
//形成拖拽效果,移动+拷贝的组合效果
}
}
///<summary>
/// Gets and sets the DataGridView to which apply filtering capabilities.
/
//</summary>
///<remarks>
///<para>
/// When a <b>DataGridView</b> is attached, the manager perform the following actions:
///<ul>
///<li>it creates a <i>filter host</i>, that is an instance of the <b>DgvFilterHost</b> class. If you previously provided a
///<i>filter host</i>, this step is skipped.</li>
///<li>it creates an array of <b>DgvBaseColumnFilter</b>, one per column, and initializes each element to a specialization
/// of <b>DgvBaseColumnFilter</b>. If <see cref="DgvFilterManager.AutoCreateFilters"/> is false, this step is skipped.
/
//</li>
///</ul>
///</para>
///<para>
/// You can force a specific <i>column filter</i> for a certain column, intervening in this process through the events
///<see cref="DgvFilterManager.ColumnFilterAdding"/> and <see cref="DgvFilterManager.ColumnFilterAdded"/>. You can also intervene, after the entire process, replacing /// a <i>column filter</i> instance in the array with another instance you created.
///</para>
///</remarks>
public DataGridView DataGridView
{
get
{
return mDataGridView;
}
set
{
mDataGridView = value;
mColumnFilterList = new List<DgvBaseColumnFilter>(mDataGridView.Columns.Count);
FindDataView();
mDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
mDataGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(mDataGridView_CellMouseClick);
mDataGridView.CellPainting += new DataGridViewCellPaintingEventHandler(mDataGridView_CellPainting);
mDataGridView.ColumnAdded += new DataGridViewColumnEventHandler(mDataGridView_ColumnAdded);