' Recently there is a forum Netizen asked: DataGridView How to implement the column header with data filtering function, like Excel advanced filtering function like ' Tonight just idle Nothing, and before also useless to this demand, so wrote a simulation function, for the altar friends as appropriate reference. ' vb.net 2008 Environment ' After creating a new project, just pull a DataGridView in the Form1, a ComboBox, then copy and paste the code below, and nothing else to do 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.even Targs) Handles MyBase.Load datagridview1.columncount = 6 DataGridView1.Rows.Add (Ten) Datagridview1.all Owusertoaddrows = False for i as Integer = 0 to Me.datagridview1.columns.count-1 Me.DataGridView1.Colu MNS (i). SortMode = datagridviewcolumnsortmode.notsortable Next ' All code is just to add the test data required for i = 0 to DataGridView1 . RowCount-1 for j as Integer = 0 to Datagridview1.columncount-1 datagridview1.columns (j). HeaderText = "& j.tostring &" column "DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = Datag Ridviewcontentalignment.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 = "Test" End If Next End Sub Private Sub Datagridview1_columnheadermouseclick (ByVal sender A S Object, ByVal e as System.Windows.Forms.DataGridViewCellMouseEventArgs) _ Handles Datagridview1.columnheadermouse Click ' Here is a key part of simulating excel sorting Selectedcol = E.columnindex Dim range as New System.Drawing.Rectangle Dim Dleft, dtop as Double range = Datagridview1.getcelldisplayrectangle (E.columnindex, E.rowindex, False) d left = 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 ("Choose Sort") 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 ' Here is the filter function 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 Implement column headers no matter what. Posted on May 31, 2013 20:02 Paste new Code Clone branch
DataGridView How to implement column header with data filtering, just like the Excel Advanced filtering feature