Solved

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

Posted on 2013-01-04
5
303 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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…

691 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