Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net web application 3 58
Query in AngularJS 8 84
Using sample Autorize.net c# simple example 1 57
Delete command SQLdatasource in asp.net, vb 2 29
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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