【问题标题】:How to use ColumnName in GridView control to hide some columns如何在 GridView 控件中使用 ColumnName 隐藏某些列
【发布时间】:2011-03-28 08:10:56
【问题描述】:

我想在显示之前隐藏 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;

}

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    //这是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";
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 2014-12-16
      • 2010-11-04
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多