Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

14 Experts available now in Live!

Get 1:1 Help Now