Solved c# gridview

Posted on 2007-10-11
Last Modified: 2013-11-26
i used gridview wizard to create data list and it works fine. However, I want to have make some condition when the gridview is looping. e.g.
when the gridview is running, I want to show if statusid = 1 then hyperlink = "accept" else statusid = 2 show something else...
how can I do that?

500 points for completed working code...

This is 2.0 c#
Question by:riskyricky1972
    LVL 16

    Expert Comment

    sorry, your request is not very clear

    do you always want to display a hyperlink but vary the Link text dependant on one of the values from the data source?

    or do you want to display a hyperlink if statusid=1 and then some other sort of control if satusid = 2?
    LVL 16

    Accepted Solution

    Ok so I'm guessing at a solution

                    <asp:HyperLink runat="server" ID="hlLink" NavigateUrl="~/Default.aspx" Text="Accept" Visible='<%# ((int)Eval("StatusId") == 1) %>' />
                    <asp:Label runat="server" ID="lblOther" Text="Other" Visible='<%# ((int)Eval("StatusId") == 2) %>' />

    The above code will show a hyperlink if StatusId = 1 and a "Other" Label if StatusId = 2
    LVL 2

    Assisted Solution

    You can achieve this  in codebehind.
    Handle this functionality in  OnRowDataBound Event of gridview.
    take hyperlink control and one more control say label in template column in gridview.

    protected void gvAltLoc_RowDataBound(object sender, GridViewRowEventArgs e)

    if (e.Row.RowType == DataControlRowType.DataRow)
                if (statuscode == "1")
                  // make label invisible
                      //make hyperlink invisible
    LVL 16

    Expert Comment

    Both solutions might be valid, one works from declarative code the other from code behind. I think me and the other poster both guessed at solutions as we where unable to get a response from riskyricky1972
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    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

    14 Experts available now in Live!

    Get 1:1 Help Now