views:

31

answers:

1

I want to hide few columns of a gridview before they gets displayed. I want to do it by create a common function which can be used by multiple controls. I am using an extension and would like to know how it can be done.

Here is my code

protected void btnStandardView_Click(object sender, EventArgs e)
{
    _viewTypeDl = new ViewTypeDL();
    DataTable dt = _viewTypeDl.GetStandardView();
    gvViewType.Source(_viewTypeDl.GetStandardView(),"ColorCode");
    ViewState["request"] = "Standard View";
}

public static void Source(this CompositeDataBoundControl ctrl, DataTable dt, params string[] ColumnsToHide)
{
    ctrl.DataSource = dt;
    ctrl.DataBound += new GridViewRowEventHandler(ctrl_DataBound);

    ctrl.DataBind();
}

static void ctrl_DataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells["ColorCode"].Visible = false;

}

I want to create an extension to hide or show columns provided in the list as an array. 1st function is used on page. While below two functions are needs to be used for multiple applications

A: 

There are two ways you can meet your requirement.

  1. set gvViewType.Columns[i].visble = false;

  2. Allow css to handle the hidden columns for you.

    .hidden
    {
        display:none;
    }
    .visble
    {
        display:block;
    }
    

//This is the Gridview event.

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         //Cells Represent the Column
         e.Row.Cells[0].CssClass = "hidden";
     }
     else if (e.Row.RowType == DataControlRowType.Header)
     {
         e.Row.Cells[0].CssClass = "hidden";
     }
}
Jordan Johnson