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

x
?
Solved

Looping GridView Cell values

Posted on 2013-05-10
4
Medium Priority
?
469 Views
Last Modified: 2013-05-10
I am trying to loop though a GridView and read values from the cells. I have no problem reading from a bound field, but how do i read from a Template field, no other controls exist inside these template fields.

I want to avoid using a label or other controls.


for (int i = 0; i < this.gv.PageCount; i++)
{
    foreach (GridViewRow row in this.gv.Rows)
                {
                        for (int j = 0; j < 6; j++)
                        {
                            row.Cells[j].Text
                         }
                 }
}

Open in new window

0
Comment
Question by:proginc
[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
  • 2
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 39157151
Can you post sample markup of your templates?

Templates contains static HTML, Web controls, and data-binding syntax so that gridview can render it, thus you need a control to get the information that is displayed, for example check:
http://stackoverflow.com/questions/9523975/how-to-capture-the-value-of-a-gridview-template-field
0
 
LVL 1

Author Comment

by:proginc
ID: 39157296
Here is the HTML markup, the bound field gets the data correctly, but the ItemTemplate does not, you are correct about using a control, but i am trying to avoid changing the HTML code.

<asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" SortExpression="EmployeeName" />
                
                <asp:TemplateField HeaderText="Average Time<br/>(in hours)" FooterStyle-Font-Bold="True"  SortExpression="AverageTime" >
                <ItemTemplate>
                  <%# GetAverageTime(Eval("AverageTime").ToString())%>
                </ItemTemplate>
                <FooterTemplate>
                  <%# GetTotalAverageTime()%>
                </FooterTemplate>
                    <FooterStyle Font-Bold="True" />
                </asp:TemplateField>

Open in new window

0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 39157456
In this case you need to use a asp.net label, the change is required because the FindControl method will not help you to get the information.
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server"
            Text='<%# GetAverageTime(Eval("AverageTime").ToString()) %>'></asp:Label>
    </ItemTemplate>
 

Open in new window


For template usage, check:
http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs
0
 
LVL 1

Author Comment

by:proginc
ID: 39157553
I was trying to avoid using a label, but that was the only option available. Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline

610 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