Solved

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

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

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

24 Experts available now in Live!

Get 1:1 Help Now