Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-12-01
5
Medium Priority
?
536 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
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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 …
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

886 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