How to use ColumnName in GridView control to hide some columns
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
There are two ways you can meet your requirement.
set gvViewType.Columns[i].visble = false;
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";
}
}
精彩评论