2010-08-11 99 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"; 
    } 
}