博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样
阅读量:6687 次
发布时间:2019-06-25

本文共 3458 字,大约阅读时间需要 11 分钟。

'近日有本论坛网友问:DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样'今晚正好闲着没事,加之以前也没用到过这个需求,所以就写了个模拟功能,供各位坛友酌情参考。'VB.NET 2008 环境'新建一个项目后,只需在Form1中拉一个DataGridView,一个ComboBox,然后将下面代码复制粘贴即可,其它什么也不用做Public Class Form1    Dim SelectedCol As Integer = 0, IsFindit As Boolean = True    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        DataGridView1.ColumnCount = 6        DataGridView1.Rows.Add(10)        DataGridView1.AllowUserToAddRows = False        For i As Integer = 0 To Me.DataGridView1.Columns.Count - 1            Me.DataGridView1.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable        Next        '以下所有代码只是为了添加演试数据需要        For i = 0 To DataGridView1.RowCount - 1            For j As Integer = 0 To DataGridView1.ColumnCount - 1                DataGridView1.Columns(j).HeaderText = "第 " & j.ToString & " 列"                DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter                DataGridView1.Rows(i).Cells(j).Value = (i + 5) * (j + 2)            Next            If i Mod 2 = 0 Then                DataGridView1.Rows(i).Cells(2).Value = "ds"            Else                DataGridView1.Rows(i).Cells(3).Value = "测试一下"            End If        Next    End Sub    Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _        Handles DataGridView1.ColumnHeaderMouseClick        '这里是模拟EXCEL排序的关键部分        SelectedCol = e.ColumnIndex        Dim range As New System.Drawing.Rectangle        Dim dLeft, dTop As Double        range = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)        dLeft = range.Left + DataGridView1.Left        dTop = range.Top + DataGridView1.Top        ComboBox1.SetBounds(dLeft, dTop, range.Width, range.Height)        ComboBox1.Items.Clear()        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList        ComboBox1.Items.Add("请选择排序")        For i As Integer = 0 To DataGridView1.RowCount - 1            IsFindit = False            For j = 0 To ComboBox1.Items.Count - 1                If ComboBox1.Items(j).ToString = DataGridView1.Rows(i).Cells(e.ColumnIndex).Value.ToString Then                    IsFindit = True                    j = ComboBox1.Items.Count                End If            Next            If Not IsFindit Then ComboBox1.Items.Add(DataGridView1.Rows(i).Cells(e.ColumnIndex).Value.ToString)        Next        ComboBox1.SelectedIndex = 0        ComboBox1.Show()    End Sub    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged        '这里是筛选功能        Select Case ComboBox1.SelectedIndex            Case 0                For i As Integer = 0 To DataGridView1.RowCount - 1                    DataGridView1.Rows(i).Visible = True                Next            Case Else                For i As Integer = 0 To DataGridView1.RowCount - 1                    If DataGridView1.Rows(i).Cells(SelectedCol).Value.ToString <> ComboBox1.SelectedItem.ToString Then                        DataGridView1.Rows(i).Visible = False                    Else                        DataGridView1.Rows(i).Visible = True                    End If                Next        End Select        If ComboBox1.SelectedIndex = 0 Then ComboBox1.Hide()    End SubEnd Class[VB.net]DataGridView实现列标头不说也罢 张贴于 2013年5月31日 20:02贴新代码 克隆分支

  

转载地址:http://vhhao.baihongyu.com/

你可能感兴趣的文章
身份证验证
查看>>
K条最短路径算法(KSP, k-shortest pathes):Yen's Algorithm
查看>>
mysql last_insert_id() (转载)
查看>>
eclipse安装反编译插件jadclipse
查看>>
Change the Forwarding: RMT Architecture
查看>>
P1040 加分二叉树
查看>>
MySQL数据库安装(CentOS操作系统/tar.gz方式)
查看>>
Maven详解(八)------ 继承和聚合
查看>>
iOS开发ARC内存管理技术要点
查看>>
spring4.0之一:简介
查看>>
Control character in cookie value or attribute
查看>>
kali linux2.0安装vega
查看>>
我的第一个python web开发框架(6)——第一个Hello World
查看>>
Windows下swoole扩展的编译安装部署
查看>>
你只是假装很努力
查看>>
ipc 进程间通讯的AIDL
查看>>
C语言版——点亮LED灯,深入到栈
查看>>
安装setuptools和pip
查看>>
C#使用BeginInvoke和EndInvoke异步下载和获取返回结果
查看>>
MII_GMII_RGMII_RMII_SMII_SSMII_TBI_RTBI
查看>>