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

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

 更新時(shí)間:2023年09月14日 10:22:34   作者:Csharp 小記  
這篇文章主要為大家詳細(xì)介紹了Winform如何使用原生DataGridView實(shí)現(xiàn)下拉篩選功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下

前言

在原生控件DataGridView中實(shí)現(xiàn)點(diǎn)擊表頭篩選的功能;其實(shí)很多第三方控件的表格都有這個(gè)功能,我也經(jīng)常會(huì)在群里推薦大家使用第三方,因?yàn)楹?jiǎn)單方便。不過(guò)也免不了破解或者收費(fèi)的問(wèn)題,而且很多時(shí)候會(huì)覺(jué)得再引用第三方導(dǎo)致項(xiàng)目重量化了。所以本文寫了一個(gè)簡(jiǎn)單的實(shí)現(xiàn)方式。樣式不太美觀,可自己根據(jù)需求愛好調(diào)整。

開發(fā)環(huán)境:.NET Framework版本:4.8

開發(fā)工具:Visual Studio 2022

實(shí)現(xiàn)步驟

1.首先創(chuàng)建一個(gè)用來(lái)篩選的自定義控件面板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)
                        {
                            //存儲(chǔ)被篩選掉的數(shù)據(jù)
                            FilterData[hit.ColumnIndex].Add(DeepCopy(GridView.Rows[i]));
                            //清除被篩選掉的數(shù)據(jù)
                            GridView.Rows.RemoveAt(i);
                            i--;
                        }
                    }
                }
                //篩選完后隱藏
                Hide();
            }
        }

3.使用的時(shí)候只需要將當(dāng)前對(duì)應(yīng)的DataGridView傳給面板控件即可,同時(shí)需要處理列頭的點(diǎn)擊事件

4.為了方便使用,這里使用自定義控件對(duì)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

开江县| 集安市| 辽源市| 黔南| 江安县| 长丰县| 开平市| 绥化市| 客服| 张家港市| 旌德县| 南召县| 郁南县| 三台县| 五家渠市| 赤城县| 柯坪县| 聊城市| 苏尼特左旗| 武山县| 惠来县| 五大连池市| 叙永县| 秭归县| 博客| 丹东市| 平远县| 大英县| 营口市| 雷波县| 宁陵县| 沾益县| 阿拉善盟| 哈巴河县| 珲春市| 苏州市| 三台县| 镶黄旗| 南安市| 海晏县| 乌鲁木齐市|