如何在GridView控件中使用Column Name来隐藏一些列

时间:2010-08-11 11:53:39

标签: asp.net data-binding gridview controls event-handling

我希望在显示gridview之前隐藏几列。 我想通过创建一个可以由多个控件使用的通用函数来实现。 我正在使用扩展程序并想知道如何完成。

这是我的代码

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;

}

我想创建一个扩展来隐藏或显示列表中提供的列作为数组。 第一个功能在页面上使用。虽然以下两个功能需要用于多个应用程序

1 个答案:

答案 0 :(得分:1)

有两种方法可以满足您的要求。

  1. 设置gvViewType.Columns [i] .visble = false;

  2. 允许css为您处理隐藏的列。

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

  3. //这是Gridview事件。

    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";
         }
    }