GridView屬性中AutoGenerateColumns設為False時,內容欄位格式均為自行設計Coding,故若有Primary Key或是欲隱藏不顯示的欄位內容,但欄位值本身仍需做計算或供其他網頁使用,可採用以下方式,將欄位標題及內容隱藏,不顯示於畫面上,但該欄位所有的數值或文字,仍可Coding使用。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// 隱藏標題列中某特定欄位
// 1
e.Row.Cells[3].Visible = false;
// 2
foreach (TableCell col in e.Row.Cells)
{
if ((((DataControlFieldCell)(col)).ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 3
foreach (DataControlFieldCell col in e.Row.Cells)
{
if ((col.ContainingField).HeaderText == "標題文字4")
{
col.Visible = false;
}
}
// 4(using System.Linq;) Page_Load中可使用
e.Row.Cells.Cast()
.Where(c => c.Text == "標題文字4")
.ToList()
.ForEach(col => col.Visible = false);
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
文章標籤
全站熱搜
