在DataGridView控件上实现列标头象Excel带数据筛选功能
来源:互联网 发布:windows 关键问题 编辑:程序博客网 时间:2024/06/05 00:50
'VB.NET 2008 环境
'新建一个项目后,只需在Form1中拉一个DataGridView,一个ComboBox,然后将下面代码复制粘贴即可,其它什么也不用做
Public Class Form1
Dim SelectedCol As Integer = 0, IsFindit As Boolean = True
Private Sub Form1_Load(ByVal senderAs System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.ColumnCount = 6
DataGridView1.Rows.Add(10)
DataGridView1.AllowUserToAddRows = False
For i AsInteger = 0To 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 Mod2 = 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 senderAs 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 AsInteger = 0To 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.ToStringThen
IsFindit = True
j = ComboBox1.Items.Count
End If
Next
If Not IsFinditThen ComboBox1.Items.Add(DataGridView1.Rows(i).Cells(e.ColumnIndex).Value.ToString)
Next
ComboBox1.SelectedIndex = 0
ComboBox1.Show()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal senderAs 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.ToStringThen
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
'新建一个项目后,只需在Form1中拉一个DataGridView,一个ComboBox,然后将下面代码复制粘贴即可,其它什么也不用做
Public Class Form1
Dim SelectedCol As Integer = 0, IsFindit As Boolean = True
Private Sub Form1_Load(ByVal senderAs System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.ColumnCount = 6
DataGridView1.Rows.Add(10)
DataGridView1.AllowUserToAddRows = False
For i AsInteger = 0To 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 Mod2 = 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 senderAs 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 AsInteger = 0To 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.ToStringThen
IsFindit = True
j = ComboBox1.Items.Count
End If
Next
If Not IsFinditThen ComboBox1.Items.Add(DataGridView1.Rows(i).Cells(e.ColumnIndex).Value.ToString)
Next
ComboBox1.SelectedIndex = 0
ComboBox1.Show()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal senderAs 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.ToStringThen
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
- 在DataGridView控件上实现列标头象Excel带数据筛选功能
- 将Excel中数据导入DataGridView中并实现筛选功能
- 用DataGridView实现Excel列的复杂筛选功能
- 利用BindingSource.Filter筛选数据,并显示在DataGridView上
- Excel的筛选功能应用教你在大数据中筛选出需要的数据
- 在 Excel 中使用高级筛选功能
- 在winfrom中,如何将数据库的数据显示在datagridview控件上。
- vb.net datagridview实现数据导出Excel
- 解决:VB.NET程序中 datagridview 多次绑定出现 的“不能在数据绑定的 DataGridView 控件上设置 ColumnCount
- 使用 Dojo 显示表格数据,并带筛选(filter)功能
- 在 DataGridView 控件中修改数据
- C#:在DataGridView控件中显示数据
- VS2005 中如何在DataGridView控件的数据列标题上绘制编号
- 不能在数据绑定的 DataGridView 控件上设置 ColumnCount 属性
- EXCEL数据筛选高级功能之条件区域
- datagridview控件--导出Excel
- 使用Excel筛选数据
- Excel筛选重复数据
- 【hive】How to use Elephant Bird with Hive
- 代码规范小结
- linux下lrzsz安装过程,SecureCRT上传下载文件工具
- [MySQL] 主流存储引擎介绍
- httpfox 下载地址
- 在DataGridView控件上实现列标头象Excel带数据筛选功能
- 好久没有写博客了
- 接口实现状态保存(三种方法)
- Excel为整列设置函数
- 给定你一个字符串,把字符串中每个字符按不同位置打印
- 2013山东省赛D题 upcoj 2220: Rubik’s cube(爆搜)
- 有用的工具类
- MySQL:如何编写UDF
- 语言栏和声音图标消失的解决方案