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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
They're all bound fields.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
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?
ASKER
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.
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.
ASKER
Here's a workaround
Protected Sub GridView1_RowDataBound(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) 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.
Protected Sub GridView1_RowDataBound(ByV
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.
ASKER
Wouldna dunnit as fast without the prompting, thus the points and score. Thanks!
ASKER