Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now