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软件对 ...
随机推荐
- how to increase an regular array length in java?
Arrays in Java are of fixed size that is specified when they are declared. To increase the size of t ...
- php的memcache和memcached扩展区别【转载】
老生长谈的问题了.我这里就整理一下. memcache的文档在:http://pecl.php.net/package/memcache memcached的文档在:http://pecl.php.n ...
- PHP实现队列的原理
关于的队列的介绍,我这里就不多讲了,随便百度一下都很多 用过laravel框架的童鞋都知道其自带队列功能,之前我很费解,PHP只是一个脚本,有超时机制 为什么能不停的去执行队列呢? 带着这个问题,在网 ...
- go share library
http://blog.ralch.com/tutorial/golang-sharing-libraries/ Sharing Golang packages to C and Go Sun, Au ...
- Python Django Learning Notes..
The first time I came across django was last month.. Since then I was considering it as the better c ...
- 【语法】修饰符 static extern const
转载自:http://my.oschina.net/u/2560887/blog/552683 一.C语言中的static的作用 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...
- python 对比图片相似度
最近appium的使用越来越广泛了,对于测试本身而言,断言同样是很重要的,没有准确的断言那么就根本就不能称之为完整的测试了.那么目前先从最简单的截图对比来看.我这里分享下python的图片相似度的代码 ...
- hdu 3342 Legal or Not(拓扑排序)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- Linux学习 -- Shell编程 -- 流程控制
if语句 单分支 if [ 条件判断式 ]; then 程序 fi 或者 if [ 条件判断式 ] then 程序 fi 例子: 双分支 if [ 条件判断式 ] then 程序 else 程序 fi ...
- 设置自己Eclipse代码风格(内部)
http://www.cnblogs.com/farseer810/p/4391318.html 经过这几次的代码提交,发现很多人的代码风格不够规范.个人认为很有必要强制性规定一下代码的规范. 整体来 ...