Rob Sherratt
Well-known member
I Hope this code snippet might be useful to other people. I wanted to provide my users with a one-click alphabetical index mechanism to find column headings in a very large DataTable (> 5000 rows and over 200 columns of various headings displayed in a functional order but not alphabetic). I used a ListBox in my form which I renamed as "SearchList". I gave it vertical and horizontal scroll bars in Forms Designer and also set it to single cell selection mode. The user can click on any item in the alphabetical index, and the selection in the corresponding MyDataGridView is moved to that column heading, while the currently selected row is preserved. MyDataGridView is configured to only permit single cell selection. In the code snippet, MyDataTable is NOT the DataTable that is bound to MyDataGridView, but instead it just contains a single column headed "Name" and each row is one column name entry from MyDataGridView.
Regards,
Rob
Private Sub Show_Search_List() ' Implements Column heading Search capability for MyDataGridView SearchList.Sorted = True SearchList.DataSource = New DataView(MyDataSet.Tables("MyDataTable containing list of column names in MyDataGridView")) SearchList.DisplayMember = "Name" ' The first column in MyDataTable has the title "Name" SearchList.Show() End Sub Private Sub SearchList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchList.Click ' The SelectedItem in the SearchList browser will be a valid column name for MyDataGridView Dim MyColumnName As String = SearchList.Text Dim MySelRow As Integer = MyDataGridView.SelectedCells(0).RowIndex For Each cell As DataGridViewCell In MyDataGridView.SelectedCells cell.Selected = False Next cell ' This is the cell in MyDataGridView to select MyDataGridView.Rows(MySelRow).Cells(MyColumnName).Selected = True End Sub
Regards,
Rob
Last edited: