DataGridView如何实现列标头带数据筛选功能,就象Excel高级筛选功能一样
'近日有本论坛网友问: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高级筛选功能一样的更多相关文章
- pandas的筛选功能,跟excel的筛选功能类似,但是功能更强大。
Select rows from a DataFrame based on values in a column -pandas 筛选 https://stackoverflow.com/questi ...
- 获取DataGridview中某列的所有数据
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam& ...
- Python:读取txt中按列分布的数据,并将结果保存在Excel文件中 && 保存每一行的元素为list
import xlwt import os def write_excel(words,filename): #写入Excel的函数,words是数据,filename是文件名 wb=xlwt.Wor ...
- EXCEL应用:高级筛选里的条件或和与的条件怎么写 例:不包含,包含等
============================================================= a列包含b列,在c列中显示b列信息, =INDEX(B:B,MIN(IF(I ...
- DataGridView绑定数据库,取得的数据插入到DataGridView指定列(一)
实现: 点击button1,从数据库中获得数据,指定数据库的某列数据插入到DataGridView指定列 一.双击button1进入事件代码 private void button1_Click(ob ...
- DataGridView固定了列名,怎样将数据内容绑定在列上
留心驿站 原文 其实很简单,在DataGridView上右键选择编辑列,在数据一项中找到DataPropertyName,在里面写上对应的要绑定的数据中的字段名,即可 .比如:从数据库中选择的data ...
- DataGridView列标题(列标头)不能居中的解决方法
winform DataGridView列标题(列标头)不能完全居中的解决方法,一般列标题的居中我们都使用 DgvDemo.ColumnHeadersDefaultCellStyle.Alignmen ...
- excel比较筛选两列不一样的数据
在excel表中,罗列两列数据,用B列数据与A列比较,筛选出B列中哪些数据不同,并用红色标记出来. 首先选中B列.直接鼠标左键点击B列即可选中."开始"--->&qu ...
- Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010
摘 要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...
随机推荐
- 交互式shell和非交互式shell的区别
交互式模式就是shell等待你的输入,并且执行你提交的命令.这种模式被称作交互式是因为shell与用户进行交互.这种模式也是大多数用户非常熟悉的:登录.执行一些命令.签退.当你签退后,shell也终止 ...
- LeetCode OJ 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- javascript: 字符串拼接有问题
点击某个链接,出现js错误:Uncaught SyntaxError: Unexpected token ILLEGAL 原因: $str .= sprintf('   ...
- network: 思科-华为光模块
思科-华为光模块的分类比较 摘要:本文介绍:思科GLC-SX-MM,GLC-LH-SM光模块等产品参数与图片,华为光模块的型号与分类等知识. 光模块分类与介绍 一.思科厂家 1.多模光模块 型号: ...
- IOS抓包工具paros的使用
一,环境安装,软件安装 1.下载安装java JDK for Mac,可以从Oracle官网下载(但是一般Oracle的都是最新的版本的),或者从苹果的开发者网站下载,我是从苹果官网下载的javafo ...
- wpf资源嵌套,一个资源引用另外一个资源,被引用的资源应该声明在前面
在wpf的XAML的Window.Resources中,一个资源引用另外一个资源,出现如下错误: “错误 1 “{DependencyProperty.UnsetValue}”不是 Setter 上“ ...
- linux fork()函数
C语言编程创建函数fork() 执行解析 | 浏览:1842 | 更新:2013-04-22 15:12 | 标签:c语言 概述 最近在看进程间的通信,看到了fork()函数,虽然以前用过,这次经过思 ...
- ANT 操控 ORACLE数据库实践
Ant 执行系统命令没有任何问题,这次实际系统命令中可以说遇到了两个问题,一个是启动服务的命令是含有空格的,第二个如何备份数据库可以自动加上日期. 首先,我们启动oracle数据库,操作有两个: 1. ...
- hadoop中联结不同来源数据
装载自http://www.cnblogs.com/dandingyy/archive/2013/03/01/2938462.html 有时可能需要对来自不同源的数据进行综合分析: 如下例子: 有Cu ...
- js遍历table 和 jquery 遍历table
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...