Winform使用DataGridView實現(xiàn)下拉篩選
前言
在原生控件DataGridView中實現(xiàn)點擊表頭篩選的功能;其實很多第三方控件的表格都有這個功能,我也經(jīng)常會在群里推薦大家使用第三方,因為簡單方便。不過也免不了破解或者收費的問題,而且很多時候會覺得再引用第三方導致項目重量化了。所以本文寫了一個簡單的實現(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.使用的時候只需要將當前對應的DataGridView傳給面板控件即可,同時需要處理列頭的點擊事件
4.為了方便使用,這里使用自定義控件對DataGridView進行繼承重載。也可以直接在窗體中調(diào)用事件處理
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();
}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)效果

到此這篇關于Winform使用DataGridView實現(xiàn)下拉篩選的文章就介紹到這了,更多相關Winform DataGridView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#利用System.Threading.Thread.Sleep即時輸出信息的詳解
本篇文章是對C#利用System.Threading.Thread.Sleep即時輸出信息進行了詳細的分析介紹,需要的朋友參考下2013-06-06
C#實現(xiàn)JWT無狀態(tài)驗證的實戰(zhàn)應用解析
這篇文章主要介紹了C#實現(xiàn)JWT無狀態(tài)驗證的實戰(zhàn)應用解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
C#過濾DataTable中空數(shù)據(jù)和重復數(shù)據(jù)的示例代碼
這篇文章主要給大家介紹了關于C#過濾DataTable中空數(shù)據(jù)和重復數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
winform下實現(xiàn)win7 Aero磨砂效果實現(xiàn)代碼
winform下實現(xiàn)win7 Aero磨砂效果實現(xiàn)代碼,需要的朋友可以參考下2012-03-03
C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例
這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長隆歡樂世界問題的具體實現(xiàn)技巧,需要的朋友可以參考下2017-09-09

