Winform使用DataGridView實(shí)現(xiàn)下拉篩選
前言
在原生控件DataGridView中實(shí)現(xiàn)點(diǎn)擊表頭篩選的功能;其實(shí)很多第三方控件的表格都有這個功能,我也經(jīng)常會在群里推薦大家使用第三方,因?yàn)楹唵畏奖恪2贿^也免不了破解或者收費(fèi)的問題,而且很多時候會覺得再引用第三方導(dǎo)致項(xiàng)目重量化了。所以本文寫了一個簡單的實(shí)現(xiàn)方式。樣式不太美觀,可自己根據(jù)需求愛好調(diào)整。
開發(fā)環(huán)境:.NET Framework版本:4.8
開發(fā)工具:Visual Studio 2022
實(shí)現(xiàn)步驟
1.首先創(chuàng)建一個用來篩選的自定義控件面板GridFilterPanel
2.在面板控件中分別實(shí)現(xiàn)顯示、隱藏以及篩選事件處理
/// <summary> /// 顯示篩選面板 /// </summary> public new void Show() { if (GridView == null) { return; } //獲取點(diǎn)擊的列 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> /// 點(diǎn)擊篩選事件 /// </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(); //獲取點(diǎn)擊的列 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
傳給面板控件即可,同時需要處理列頭的點(diǎn)擊事件
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;
實(shí)現(xiàn)效果
到此這篇關(guān)于Winform使用DataGridView實(shí)現(xiàn)下拉篩選的文章就介紹到這了,更多相關(guān)Winform DataGridView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#基于NVelocity實(shí)現(xiàn)代碼生成
這篇文章主要介紹了c#基于NVelocity實(shí)現(xiàn)代碼生成的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01C#通過WIN32 API實(shí)現(xiàn)嵌入程序窗體
這篇文章主要介紹了C#通過WIN32 API實(shí)現(xiàn)嵌入程序窗體的方法,涉及WIN32 API的調(diào)用及窗體的設(shè)計(jì),具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)利用反射簡化給類字段賦值的方法
這篇文章主要介紹了C#實(shí)現(xiàn)利用反射簡化給類字段賦值的方法,涉及C#操作反射的相關(guān)技巧,需要的朋友可以參考下2015-05-05C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐
有時我們可能會遇到需要的資料或教程被分成了幾部分存放在多個PDF文件中,本文主要介紹了C#實(shí)現(xiàn)PDF合并的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01C#中實(shí)現(xiàn)Fluent Interface的三種方法
這篇文章主要介紹了C#中實(shí)現(xiàn)Fluent Interface的三種方法,本文講解了Fluent Interface的簡單實(shí)現(xiàn)、使用裝飾器模式和擴(kuò)展方法實(shí)現(xiàn)Fluent Interface等3種實(shí)現(xiàn)方法,需要的朋友可以參考下2015-03-03