Solved

Looping GridView Cell values

Posted on 2013-05-10
4
462 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

737 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