[研究][ASP.NET]GridView如何動態隱藏 BoundField 或 TemplateField 欄位不顯示?
2025-02-12
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
(舊文翻出,不太記得是否測過)
********************************************************************************
範例1:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // 根據欄位名稱尋找欄位控制項 BoundField field = GridView1.Columns.Cast<DataControlField>().Where(f => f.HeaderText == "ColumnName").OfType<BoundField>().FirstOrDefault(); if (field == null) { TemplateField tempField = GridView1.Columns.Cast<DataControlField>().Where(f => f.HeaderText == "ColumnName").OfType<TemplateField>().FirstOrDefault(); if (tempField != null) { // 如果是 TemplateField,尋找其內部的控制項 Label label = e.Row.FindControl("Label1") as Label; if (label != null) { // 隱藏控制項 label.Visible = false; } } } else { // 如果是 BoundField,尋找其對應的 TableCell TableCell cell = e.Row.Cells[GridView1.Columns.IndexOf(field)]; if (cell != null) { // 隱藏 TableCell cell.Visible = false; } } } } |
在這個範例中,我們使用 Cast 方法將 GridView 的 Columns 屬性轉換為 DataControlField 集合,然後使用 Where 方法根據欄位名稱找到對應的欄位控制項。最後,我們使用 OfType 方法將 BoundField 或 TemplateField 強制轉換成對應的類型,並使用 FindControl 方法尋找內部控制項。如果是 BoundField,則需要使用 Cells 屬性尋找對應的 TableCell。
如果是 DetailsView 控制項,可以在 DataBound 事件中使用相同的方式尋找欄位控制項並隱藏它們。
********************************************************************************
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // 尋找欄位控制項 BoundField field = GridView1.Columns[0] as BoundField; if (field == null) { TemplateField tempField = GridView1.Columns[0] as TemplateField; if (tempField != null) { // 如果是 TemplateField,尋找其內部的控制項 Label label = e.Row.FindControl("Label1") as Label; if (label != null) { // 隱藏控制項 label.Visible = false; } } } else { // 如果是 BoundField,尋找其對應的 TableCell TableCell cell = e.Row.Cells[0]; if (cell != null) { // 隱藏 TableCell cell.Visible = false; } } } } |
在這個範例中,我們首先檢查欄位是否為 BoundField 或 TemplateField,並使用 FindControl 方法尋找對應的控制項。如果是 TemplateField,則需要尋找其內部的控制項。最後,我們設定控制項或 TableCell 的 Visible 屬性為 false,以隱藏欄位。注意在 DetailsView 控制項中,欄位控制項需要使用 Fields 屬性來尋找,而資料列控制項需要使用 Rows 屬性來尋找。
********************************************************************************
按下 Button 後,如何讓 GridView動態隱藏 BoundField 或 TemplateField 欄位不顯示?
若您使用 ASP.NET Web Forms,您可以在按下 Button 的事件處理程式中,設定 GridView 的相應 BoundField 或 TemplateField 的 Visible 屬性為 false,以隱藏該欄位。
以下是一個示範程式碼,假設您有一個 GridView 控制項名為 "GridView1",其中包含一個 BoundField 名為 "CustomerName":
範例3:
protected void Button1_Click(object sender, EventArgs e) { GridView1.Columns[0].Visible = false; // 隱藏第一個欄位 } |
若您要隱藏的欄位是一個 TemplateField,您可以指定該欄位的 Index 以隱藏:
範例4:
protected void Button1_Click(object sender, EventArgs e) { GridView1.Columns[1].Visible = false; // 隱藏第二個欄位 } |
請注意,這個示範程式碼中的 Index 值是從 0 開始算起的,所以第一個欄位的 Index 為 0,第二個欄位的 Index 為 1,以此類推。
********************************************************************************
能根據 BoundField 的 DataField,或 TemplateField 某屬性值,進行隱藏 ?
是的,您可以使用 BoundField 或 TemplateField 的 DataField 或某些屬性值來識別要隱藏的欄位,然後將 Visible 屬性設定為 false。
下面是一個示範程式碼,其中假設您有一個 GridView 控制項名稱為 "GridView1",其中包含一個 BoundField 名稱為 "CustomerName",您可以使用 BoundField 的 DataField 屬性來找到要隱藏的欄位:
範例5:
protected void Button1_Click(object sender, EventArgs e) { // 找到名稱為 "CustomerName" 的 BoundField BoundField field = GridView1.Columns.OfType<BoundField>().FirstOrDefault(f => f.DataField == "CustomerName"); if (field != null) { field.Visible = false; // 隱藏該欄位 } } |
如果您要隱藏的是一個 TemplateField,則可以根據您在其屬性中使用的值來找到該欄位。例如,假設您的 TemplateField 包含一個 Label 控制項,其 ID 為 "CustomerNameLabel",您可以使用 TemplateField 的 HeaderTemplate 或 ItemTemplate 屬性來找到該 Label 控制項,然後將它隱藏:
範例6:
protected void Button1_Click(object sender, EventArgs e) { // 找到包含 ID 為 "CustomerNameLabel" 的 Label 控制項的 TemplateField TemplateField field = GridView1.Columns.OfType<TemplateField>().FirstOrDefault(f => { Label label = f.HeaderTemplate?.FindControl("CustomerNameLabel") as Label ?? f.ItemTemplate?.FindControl("CustomerNameLabel") as Label; return label != null; }); if (field != null) { field.Visible = false; // 隱藏該欄位 } } |
在這個示範程式碼中,我們在 HeaderTemplate 和 ItemTemplate 中查找 ID 為 "CustomerNameLabel" 的 Label 控制項,並使用合併運算子 (??) 指定如果找不到 Label 控制項,則返回 null。如果找到了 Label 控制項,我們就可以得到包含它的 TemplateField,然後將其 Visible 屬性設定為 false。
(完)
相關
沒有留言:
張貼留言