Solved

Looping GridView Cell values

Posted on 2013-05-10
4
453 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
  • 2
  • 2
4 Comments
 
LVL 35

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 35

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Any Third party toll for Integrating chat system on ASP.NET ? 2 34
Runtime Exceptions when trying to submit data 28 37
C# Single Form 8 29
Entity Framework 7 32
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

867 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

21 Experts available now in Live!

Get 1:1 Help Now