?
Solved

Looping GridView Cell values

Posted on 2013-05-10
4
Medium Priority
?
467 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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