前言
在原生控件DataGridView中实现点击表头筛选的功能;其实很多第三方控件的表格都有这个功能,我也经常会在群里推荐大家使用第三方,因为简单方便。不过也免不了破解或者收费的问题,而且很多时候会觉得再引用第三方导致项目重量化了。所以本文写了一个简单的实现方式。样式不太美观,可自己根据需求爱好调整。
开发环境:.NET Framework版本:4.8
开发工具:Visual Studio 2022
实现步骤
首先创建一个用来筛选的自定义控件面板
GridFilterPanel
在面板控件中分别实现显示、隐藏以及筛选事件处理
/// <summary>
/// 显示筛选面板
/// </summary>
public new void Show()
{
if (GridView == null) { return; }
//获取点击的列
Point point = GridView.PointToClient(Cursor.Position);
DataGridView.HitTestInfo hit = GridView.HitTest(point.X, point.Y);
if (hit.ColumnIndex > -1)
{
//加入到筛选面板中
list_filter.Items.Clear();
List<string> items = new List<string>();
foreach (DataGridViewRow row in GridView.Rows)
{
string value =Convert.ToString(row.Cells[hit.ColumnIndex].Value);
if (!items.Contains(value))
{
items.Add(value);
}
}
list_filter.Items.AddRange(items.ToArray());
//定位筛选面板的位置
Location = new Point(hit.ColumnX, hit.RowY);
Visible = true;
}
}
/// <summary>
/// 隐藏筛选面板
/// </summary>
public new void Hide()
{
if (Visible)
{
Visible = false;
}
}
/// <summary>
/// 点击筛选事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void list_filter_SelectedIndexChanged(object sender, EventArgs e)
{
if (list_filter.SelectedIndex > -1)
{
string selectValue = list_filter.Items[list_filter.SelectedIndex].ToString();
//获取点击的列
Point point = GridView.PointToClient(Cursor.Position);
DataGridView.HitTestInfo hit = GridView.HitTest(point.X, point.Y);
if (hit.ColumnIndex > -1)
{
for (int i = 0; i < GridView.Rows.Count; i++)
{
string value = Convert.ToString(GridView.Rows[i].Cells[hit.ColumnIndex].Value);
if (GridView.Rows[i].IsNewRow) continue;
if (selectValue != value)
{
//存储被筛选掉的数据
FilterData[hit.ColumnIndex].Add(DeepCopy(GridView.Rows[i]));
//清除被筛选掉的数据
GridView.Rows.RemoveAt(i);
i--;
}
}
}
//筛选完后隐藏
Hide();
}
}
使用的时候只需要将当前对应的
DataGridView
传给面板控件即可,同时需要处理列头的点击事件为了方便使用,这里使用自定义控件对
DataGridView
进行继承重载。也可以直接在窗体中调用事件处理
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base.OnCellPainting(e);
if (e.RowIndex == -1 && e.ColumnIndex > -1)
{
//绘制筛选图标
Rectangle rect = new Rectangle(e.CellBounds.Right - 10, 5, 5, 5);
e.Paint(rect, DataGridViewPaintParts.All);
e.Graphics.DrawString("◊", new Font("宋体", 5), Brushes.Black, rect.X, rect.Y);
e.Handled = true;
}
}
protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
{
base.OnCellMouseClick(e);
if (e.Button == MouseButtons.Left)
{
HitTestInfo hit = HitTest(e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.ColumnHeader)
{
Rectangle headerCellRect = GetColumnDisplayRectangle(hit.ColumnIndex, false);
if (e.X > headerCellRect.Width - 20)
{
filterPanel.Show();
return;
}
}
}
filterPanel.Hide();
}
最后把控件拖到窗体上,然后绑定数据
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i, "name" + i);
}
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i, "name" + i + "_1");
}
gridViewFilter1.DataSource = dt;
实现效果
-技术群:添加小编微信并备注进群
小编微信:mm1552923 公众号:dotNet编程大全