Solved

how do I populate gridview label (item template) with function

Posted on 2006-12-01
5
532 Views
Last Modified: 2008-02-01
How do I populate a template field label using a method in code behind?

I attempted this: (in gridview gvCase in aspx page)
<asp:TemplateField HeaderText="case">
                 <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text="<%# getCase() %>"></asp:Label>
                    </ItemTemplate>
                 </asp:TemplateField>
[...]
in aspx.cs page

protected string getCases()
    {
        for (int index = 0; index < gvCase.Rows.Count; index++)
        {
            if (gvCase.Rows[index].RowType == DataControlRowType.DataRow)
            {
                strCase = ""; //reset for each row
                strID = gvCase.Rows[index].Cells[1].Text.ToString(); //Cell number 1 is "ID" field or datakey
                if (strID != null)
                {... do some stuff here to retrieve values - this part works}
            }
        }
        return strCase;
    }

However my values are all one row off?
0
Comment
Question by:acdagirl
[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
5 Comments
 
LVL 12

Expert Comment

by:craskin
ID: 18056788
try
for (int index = 0; gvCase.Rows.Count - 1; index++)
0
 

Author Comment

by:acdagirl
ID: 18056814
hmm, now they are two off :-) so when I reverse the logic and try

for (int index = 0; gvCase.Rows.Count +1; index++)

I get an index out of range for

if (gvCase.Rows[index].RowType == DataControlRowType.DataRow)...
0
 
LVL 12

Expert Comment

by:craskin
ID: 18056917
yeh the count is going to be the total number of rows, while the index starts at 0. so the last row, if it's 16, will have an index of 15. it looks like you already knew this.

maybe try setting it to run whether it's a datarow or an alternatingrow.
0
 
LVL 20

Accepted Solution

by:
brwwiggins earned 500 total points
ID: 18056943
what if you did it on the row databound event? Then you can easily use the findcontrol method to grab your lablel

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/GridViewEx07.asp

0
 

Author Comment

by:acdagirl
ID: 18056970
ok, this looks good... I'll read it and reply.
0

Featured Post

Independent Software Vendors: 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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

632 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