Gridview RowDataBound show hidden column for each row

I have a gridview where I only want to allow editing of a textbox if a specific list item is down selected. In my RowDataBound I get the value and decide if I need to edit it, but the .Visible attribute does not work as I expect. Using my sample data, I would expect the first and second rows to NOT have a text box in the column, the third to have a text box, and the fourth NOT to have again. Any help would be greatly appreciated.

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Control ctrl = e.Row.FindControl("ddlPriceTypeCode");

        if (ctrl != null)
        {
            DropDownList ddl = (DropDownList)ctrl;
            if (ddl.SelectedValue == "UPRC-" || ddl.SelectedValue == "PLEV-0" || ddl.SelectedValue == "PLEV-1" || ddl.SelectedValue == "PLEV-2" || ddl.SelectedValue == "PLEV-3" || ddl.SelectedValue == "PLEV-4" || ddl.SelectedValue == "PLEV-5" || ddl.SelectedValue == "PLEV-6" || ddl.SelectedValue == "PLEV-7")
            {
                //GridView1.Columns[4].Visible = true;
            }
            else 
            { 
                //GridView1.Columns[4].Visible = false;
            }
        }
    }

      

+3


source to share


2 answers


This way you hide / show the entire column. RowDataBound

fires for each row, so the visibility of the column is set by the value of the drop-down list of the last row.

What do you need to do if you only hide / show the TextBox, for example:



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    Control ctrl = e.Row.FindControl("ddlPriceTypeCode");
    TextBot txt = (TextBox)e.Row.FindControl("txtID");

    if (ctrl != null)
    {
        DropDownList ddl = (DropDownList)ctrl;
        if (ddl.SelectedValue == "UPRC-" || ddl.SelectedValue == "PLEV-0" || ddl.SelectedValue == "PLEV-1" || ddl.SelectedValue == "PLEV-2" || ddl.SelectedValue == "PLEV-3" || ddl.SelectedValue == "PLEV-4" || ddl.SelectedValue == "PLEV-5" || ddl.SelectedValue == "PLEV-6" || ddl.SelectedValue == "PLEV-7")
            txt.Visible = true;
        else 
            txt.Visible = false;
    }
}

      

Where obviously "txtID" is the id of the TextBox you want to hide / show.

+2


source


As far as I know, you are not checking the selected item in this event. This event is fired when a data source property is connected to a control. You should check this in the selectionChanged event



0


source







All Articles