Solved

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

Posted on 2006-12-01
5
530 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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