Winform使用DataGridView實現(xiàn)下拉篩選
前言
在原生控件DataGridView中實現(xiàn)點擊表頭篩選的功能;其實很多第三方控件的表格都有這個功能,我也經(jīng)常會在群里推薦大家使用第三方,因為簡單方便。不過也免不了破解或者收費的問題,而且很多時候會覺得再引用第三方導(dǎo)致項目重量化了。所以本文寫了一個簡單的實現(xiàn)方式。樣式不太美觀,可自己根據(jù)需求愛好調(diào)整。
開發(fā)環(huán)境:.NET Framework版本:4.8
開發(fā)工具:Visual Studio 2022
實現(xiàn)步驟
1.首先創(chuàng)建一個用來篩選的自定義控件面板GridFilterPanel
2.在面板控件中分別實現(xiàn)顯示、隱藏以及篩選事件處理
/// <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)
{
//存儲被篩選掉的數(shù)據(jù)
FilterData[hit.ColumnIndex].Add(DeepCopy(GridView.Rows[i]));
//清除被篩選掉的數(shù)據(jù)
GridView.Rows.RemoveAt(i);
i--;
}
}
}
//篩選完后隱藏
Hide();
}
}3.使用的時候只需要將當(dāng)前對應(yīng)的DataGridView傳給面板控件即可,同時需要處理列頭的點擊事件
4.為了方便使用,這里使用自定義控件對DataGridView進(jìn)行繼承重載。也可以直接在窗體中調(diào)用事件處理
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base.OnCellPainting(e);
if (e.RowIndex == -1 && e.ColumnIndex > -1)
{
//繪制篩選圖標(biāo)
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();
}5.最后把控件拖到窗體上,然后綁定數(shù)據(jù)
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;實現(xiàn)效果

到此這篇關(guān)于Winform使用DataGridView實現(xiàn)下拉篩選的文章就介紹到這了,更多相關(guān)Winform DataGridView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中實現(xiàn)Fluent Interface的三種方法
這篇文章主要介紹了C#中實現(xiàn)Fluent Interface的三種方法,本文講解了Fluent Interface的簡單實現(xiàn)、使用裝飾器模式和擴(kuò)展方法實現(xiàn)Fluent Interface等3種實現(xiàn)方法,需要的朋友可以參考下2015-03-03

