2017-07-08
第0個 Columns會整個隱藏不見
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView1.Columns[0].Visible = false; } |
但是前提是 GridView 的 AutoGenerateColumns 是 false,否則會出錯
********************************************************************************
第 index 個 Columns會整個隱藏不見
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false; } |
********************************************************************************
第 1 個 Columns會整個隱藏不見
protected void Page_Load(object sender, EventArgs e)
{
GridView1.HeaderRow.Cells[1].Visible = false; for (int i=0; i<GridView1.Rows.Count; i++) { GridView1.Rows[i].Cells[1].Visible = false; } } |
********************************************************************************
讓欄位中的某個 Control 隱藏不見
<%@ page language="C#" autoeventwireup="true" codebehind="Default.aspx.cs" inherits="WebApplication7.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="SELECT * FROM [Table3]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="no" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="no" HeaderText="no" InsertVisible="False" ReadOnly="True" SortExpression="no" />
<asp:TemplateField HeaderText="C1" SortExpression="C1">
<HeaderTemplate>
<asp:Label ID="Label_C1" runat="server" Text="C1Head"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("C1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label_C1" runat="server" Text='<%# Bind("C1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="C2" HeaderText="C2" SortExpression="C2" ControlStyle-CssClass="tdWrap" >
<ControlStyle CssClass="tdWrap" />
</asp:BoundField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label Label_C1 = (Label)e.Row.FindControl("Label_C1");
if (Label_C1 != null)
{
Label_C1.Visible = false;
}
}
}
}
|
如果要隱藏的是標題中的內容,可以自行添加 <HeaderTemplate> 區域做處理。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (GridView1.HeaderRow != null)
{
Label Label_Head = (Label)GridView1.HeaderRow.FindControl("C1Head");
if ( Label_Head != null )
Label_Head.Visible = false;
}
}
|
若只知道要隱藏的欄位名稱是 Id,無法確定是第幾欄位,可用下面方法
for (int i=0;i<dt.Columns.Count;i++)
{
if (dt.Columns[i].ColumnName == "Id") { ViewState["CellIndex"] = i; } } .... (略) GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer) { int index = Convert.ToInt32(ViewState["CellIndex"]); e.Row.Cells[index].Visible = false; } } |
(待續)
沒有留言:
張貼留言