Solved

Looping GridView Cell values

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl help with selection 14 44
Problem to go to Web page 2 52
Trouble with References... 5 25
Open a word document 23 15
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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