Link to home
Start Free TrialLog in
Avatar of codequest
codequest

asked on

Get Gridview header row

I tried to use this to scan the header rows of a gridview after it's been databound
but the Me.GridviewHeaderRow has the first row of data in it.    How might I get at that header data?

            'Dim wrkCell As Integer = -1
            'Dim wrkH As GridViewRow = Me.GridView1.HeaderRow
            'For n As Integer = 0 To wrkH.Cells.Count - 1
            '      If wrkH.Cells(n).Text = "ID" Then
            '            wrkCell = n
            '            Exit For
            '      End If
            'Next

            'If wrkCell = -1 Then
            '      Return -1
            'End If
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of codequest
codequest

ASKER

Yes.  I want to search for the header column number (= cell number) for the column with the header text i'm search for.  That's so that if I add or delete columns to the gridview, a function that uses the post bound Gridview row contents can find the appropriate cell within the row by looking at the header text...I don't want to change a hardwired cell number that give the position of that cell.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
They're all bound fields.
Doh!  Except that there are button fields.  I've spent the day sliding the cell number up and down as I modify the Gridview columns...seems like there should be a way to access the Gridview Header row that has the ColumnID's in it
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for input.  This is the accepted syntax, but it never finds the row type = header

Dim wrkAL As New ArrayList
For Each row As GridViewRow In Me.GridView1.Rows
If row.RowType = DataControlRowType.Header Then
wrkAL.add(row)
End If
Next
Dim wrkRow As GridViewRow = wrkAL(0)

yeah, i guess you are right, i was trying to remember it without vs in front of me, did it work for you?
No, it doesn't find the header row.   Part of the reason I expected to find the header row is that I use a routine like this in Javascript, where the GV table is just one table, including the header, so you just get row 0.  

In vb on the server, though (admittedly a completely different environment), GV.Rows seems to only include the data rows.  There's got to be a variable for GV header, however, the original code (at start of question) doesn't find it.  GV.HeaderRow seems to have the first row of data, not the column headers.
Here's a workaround

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

Select Case e.Row.RowType
Case DataControlRowType.Header
For n As Integer = 0 To e.Row.Cells.Count - 1
If e.Row.Cells(n).Text = "ID" Then
Session("GV1_IDColumn") = n
End If
Next
End Select
End Sub

Then pickup the session variable in the above routine.   Unless I'm modifying the GV row structure outside of VB (which I -think- I would notice doing :-) then this should work.
 
Wouldna dunnit as fast without the prompting, thus the points and score.  Thanks!