最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Winform使用DataGridView實現(xiàn)下拉篩選

 更新時間:2023年09月14日 10:22:34   作者:Csharp 小記  
這篇文章主要為大家詳細介紹了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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

称多县| 北票市| 和静县| 马关县| 浠水县| 湟中县| 南昌县| 霍林郭勒市| 个旧市| 齐河县| 荣昌县| 普格县| 慈利县| 双流县| 新田县| 鄄城县| 弋阳县| 珲春市| 珲春市| 定安县| 奉节县| 辽宁省| 南汇区| 临沧市| 渑池县| 本溪| 富源县| 灵山县| 榆社县| 通海县| 安多县| 大城县| 弥渡县| 巫山县| 蒲江县| 深圳市| 蓝山县| 牙克石市| 兴文县| 宕昌县| 沽源县|