Tips and tricks for .NET using ASP and VB code.

Get a list of GridView Column Headers

'Create a sorted list collection to store the column headers in alphabetical order.
Dim ColumnHeadersAndFields As New SortedList
'Create a list collection to store just the column headers in the order they appear in the grid view.
Dim ColumnHeadersOnly As New ArrayList
'Create a collection of the gridview's columns.
Dim ColumnFields As DataControlFieldCollection = GridViewToSearch.Columns
'Put the grid view's column names and associated database fields in the drop down list.
For Each ColumnField As DataControlField In ColumnFields
'When the column is bound to a field...
If TypeOf ColumnField Is BoundField Then
Dim BoundField As BoundField
BoundField = CType(ColumnField, BoundField)
'...save the column header and the bound field name
ColumnHeadersAndFields.Add(BoundField.HeaderText, BoundField.DataField)
Else
'...otherwise just save the column header.
ColumnHeadersAndFields.Add(ColumnField.HeaderText, "")
End If
ColumnHeadersOnly.Add(ColumnField.HeaderText)
ColumnInfoDropDownList.Items.Add(New ListItem(ColumnField.HeaderText, ColumnInfoDropDownList.Items.Count))
Next

No comments:

Post a Comment

Search This Blog