'近日有本论坛网友问: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 Sub
End Class
[VB.net]DataGridView实现列标头
不说也罢 张贴于 2013年5月31日 20:02
贴新代码 克隆分支

  

DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样的更多相关文章

  1. pandas的筛选功能,跟excel的筛选功能类似,但是功能更强大。

    Select rows from a DataFrame based on values in a column -pandas 筛选 https://stackoverflow.com/questi ...

  2. 获取DataGridview中某列的所有数据

    /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam& ...

  3. Python:读取txt中按列分布的数据,并将结果保存在Excel文件中 && 保存每一行的元素为list

    import xlwt import os def write_excel(words,filename): #写入Excel的函数,words是数据,filename是文件名 wb=xlwt.Wor ...

  4. EXCEL应用:高级筛选里的条件或和与的条件怎么写 例:不包含,包含等

    ============================================================= a列包含b列,在c列中显示b列信息, =INDEX(B:B,MIN(IF(I ...

  5. DataGridView绑定数据库,取得的数据插入到DataGridView指定列(一)

    实现: 点击button1,从数据库中获得数据,指定数据库的某列数据插入到DataGridView指定列 一.双击button1进入事件代码 private void button1_Click(ob ...

  6. DataGridView固定了列名,怎样将数据内容绑定在列上

    留心驿站 原文 其实很简单,在DataGridView上右键选择编辑列,在数据一项中找到DataPropertyName,在里面写上对应的要绑定的数据中的字段名,即可 .比如:从数据库中选择的data ...

  7. DataGridView列标题(列标头)不能居中的解决方法

    winform DataGridView列标题(列标头)不能完全居中的解决方法,一般列标题的居中我们都使用 DgvDemo.ColumnHeadersDefaultCellStyle.Alignmen ...

  8. excel比较筛选两列不一样的数据

    在excel表中,罗列两列数据,用B列数据与A列比较,筛选出B列中哪些数据不同,并用红色标记出来.     首先选中B列.直接鼠标左键点击B列即可选中."开始"--->&qu ...

  9. Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010

    摘  要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...

随机推荐

  1. 两台linux利用heartbeat+drbd 完美实现双机热备

    一直想做基于linux的双机热备,一直没有时间和机会.一直以为只要做双机热备的实验就必须两台机器外接一个存储.甚至一个月以前在学习keepalived的时候还在琢磨keepalvied去掉哪些条件可以 ...

  2. 作为Web开发人员,我为什么喜欢Google Chrome浏览器

    来源: http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.html 在Google Chrome浏览器出来之前,我一直使用FireF ...

  3. perl的package和module

    来源: http://www.cnblogs.com/itech/archive/2010/03/23/1692836.html 一 package 1) package 相当于C++中的namesp ...

  4. 【C#】HTTP请求GET,POST(远程证书失效)

    HTTP定义了与服务器交互的不同方法,基本方法有GET,POST,PUT,DELETE,分别对于查,该,增,删.一般情况下我们只用到GET和POST,其他两种都也可以用GET和POST来实现,很多浏览 ...

  5. sql server字段是逗号分割的id,关联明细表查询

    有时候一张表的一个字段是以逗号分割的一个字符串,分割的数字是明细表的主键id. 关联明细表查询可以这样做: ) ) --这是把areanos字段赋值给@areanos变量 set @areanos=' ...

  6. sql server数据库查询同义词

    查询数据库同义词: select * from sys.synonyms, 查询同义词个数:select count(1) from sys.synonyms

  7. Form表单的post 和get跳转区别

    post是隐示请求 ----- 安全 get显示请求不安全,会在URL上显示路径和参数

  8. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  9. UIKit控件直接显示网页文字内容

    NSString *html = @"<bold>Hello</bold> Now<br> <em>iOS</em> can cr ...

  10. Newly Setting up a CentOS-7 system

    yum install -y epel-release glibc.i686 libtools vim clang git autoconf automake w3m glibc screen the ...