Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Row.Cells.Text empty when bind Gridviewheader row to Table Row

Posted on 2013-01-04
5
Medium Priority
?
317 Views
Last Modified: 2013-01-11
                ' Create a table to contain the grid
                Dim objtable As New Table()

                ' add the header row to the table
                If grdview.HeaderRow IsNot Nothing Then
                    objtable.Rows.Add(grdview.HeaderRow)
                End If	
			
                ' add each of the data rows to the table
                For Each row As GridViewRow In grdview.Rows
                    objtable.Rows.Add(row)
                Next

Open in new window


I use snapshot above to bind gridview to a table however on boundfield of the gridview the cells.text return empty.
eg. objtable.rows(0).cells(0).Text returns empty (header position row 0) however for the rest of the rows I can get text objtable.rows(1).cells(0).Text etc

How can I get the gridview header text data. it seems to work sometimes for template field itemdata.
0
Comment
Question by:crashjam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38746011
Hi,

How you configured you HeaderRow in the GridView?

I have a GridView like this -

 <asp:GridView runat="server" ID="gridView"  AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="A" >
                <ItemTemplate>
                    <asp:Label Text='<%#Eval("Name")%>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="B">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="txt1"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="C">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="txt2" CssClass="myCss" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window


and this code seems to be working fine -

Dim table As New Table()
table.Rows.Add(gridView.HeaderRow)

Open in new window


Hope it helps u...
0
 

Author Comment

by:crashjam
ID: 38746289
The issue is when AllowSorting="True" the GridView Column (if a SortExpression) becomes an hyperlink and the Text column becomes empty.

example below RecordID Boundfield will be empty for Gridview headerrow
when objtable.rows(0).cells(0).Text is used.

<asp:GridView runat="server" ID="gridView"  AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True">
        <Columns>
<asp:BoundField DataField ="RecordID" HeaderText="RecordID" SortExpression="RecordID" />
            <asp:TemplateField HeaderText="A" >
                <ItemTemplate>
                    <asp:Label Text='<%#Eval("Name")%>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="B">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="txt1"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="C">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="txt2" CssClass="myCss" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window

.
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38749800
Hi,

Moreover, since you are using asp:TemplateField , then you may have to use FindControl instead of Text!

http://forums.asp.net/t/1736121.aspx/1

Some thing like this -

Dim txtBox As TextBox = TryCast(gridView.Rows(i).FindControl("txt1"), TextBox)
txtBox.Text - > Retrieves the text

Open in new window



Hope it helps u...
0
 

Accepted Solution

by:
crashjam earned 0 total points
ID: 38750073
To get the gridview headertext even if the column is set as sortable the following modification was done to the code by dynamically building a datarow.

' Create a table to contain the grid
Dim objtable As New Table()

Dim objDataRow As New TableRow

' add the header row to the table
If grdview.HeaderRow IsNot Nothing Then
	Dim headertext1 As String = String.Empty
	Dim rowi As Integer = 0

	For rowi = 0 To grdview.Columns.Count - 1
		headertext1 = grdview.Columns(rowi).HeaderText
		Dim objTableCell As New TableCell
		objTableCell.Text = headertext1
		objDataRow.Cells.Add(objTableCell)	
	Next					
	objtable.Rows.Add(objDataRow)
End If	

' add each of the data rows to the table
For Each row As GridViewRow In grdview.Rows
	objtable.Rows.Add(row)
Next

Open in new window

0
 

Author Closing Comment

by:crashjam
ID: 38766444
Building the TableRow TableCell to get the HeaderText from looping though the gridview's column is a method that's safe to get the headertext even if the column is sortable.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question