HowTo:TemplateField's DropDownList visible only sometimes

Last post 07-14-2007 11:44 AM by benrick. 1 replies.

Sort Posts:

  • HowTo:TemplateField's DropDownList visible only sometimes

    07-14-2007, 11:29 AM
    • Loading...
    • doron.oded
    • Joined on 06-20-2007, 7:08 PM
    • Posts 15

    Hi,

    I have a gridview, with a DropDownList TemplateField. I want this DDL (=DropDownList) to be visible by certain conditions.

    For example: column "A" = DDL, column "B" = number. I want the DDL to be visible only if the number ("B") of the record is 5.

    "A"                                    "B"

    DDL not visible                   3

    DDL visible                         5

    How can this be done?

  • Re: HowTo:TemplateField's DropDownList visible only sometimes

    07-14-2007, 11:44 AM
    Answer
    • Loading...
    • benrick
    • Joined on 07-27-2006, 11:12 AM
    • Kent, Ohio
    • Posts 341

    I recommend handling this during the RowDatabound event. To do this you will want to add this to the gridview.

    onrowdatabound="GridView1_RowDataBound"

    Then you will need to create the event handler in the code.

    protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            if (e.Row.Cells[1].Text != "5")
            {
                ddl.Visible = false;
            }
        }
    }
    You will want to find which Cell contains the number you're looking for. There are obviously other solutions, but I like this one. Smile 

    I hope that works out for you,

    Brendan

    C. Brendan Enrick
    Brendan's ASP.NET Advice Blog

    Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
    Filed under: ,
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter