How to Get the Gridview columnIndex dynamically

Hi Guys,

'''
''' Gets the index of a column using the supplied grid and column name
'''

'''
'''
'''
Private Function GetColumnIndex(ByVal grid As GridView, ByVal columnName As String) As Integer
Try
For Each column As DataControlField In grid.Columns
If TypeOf column Is BoundField Then
If CType(column, BoundField).DataField = columnName Then
Return grid.Columns.IndexOf(column)
End If
End If
Next

Return -1
Catch ex As Exception
Return -1
End Try
End Function


Use this function where ever you need GetColumnIndex(gridviewname, "column_name")

Thanks
Krishna RK