?
Solved

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

Posted on 2013-01-04
5
Medium Priority
?
311 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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