2010-08-11 34 views
0

我想在显示Gridview之前隐藏少数几列。 我想通过创建一个可以被多个控件使用的通用函数来实现它。 我正在使用扩展,想知道如何完成。如何在GridView控件中使用ColumnName来隐藏某些列

这里是我的代码

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

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

  1. set 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"; 
    } 
} 
相关问题