2020年4月27日 星期一

[研究][C#][ASP.NET][WebForm] GridView1.Columns.Count 和 GridView1.HeaderRow.Cells.Count 測試

[研究][C#][ASP.NET][WebForm] GridView1.Columns.Count 和 GridView1.HeaderRow.Cells.Count 測試

2020-04-27
2020-04-28 更新

GridView1.Columns.Count 是「非自動生成列數」。不管是否為 Visible。
GridView1.HeaderRow.Cells.Count 是「自動生成列數」和「非自動生成列數」的總和。不管是否為 Visible。
GridView1 會先顯示「非自動生成」的欄位,再顯示「自動生成」的欄位。

GridView1.Visible = false; 時,GridView1.HeaderRow.Cells[i].Visible 總是會 false。
GridView1.Visible = true; 時,GridView1.HeaderRow.Cells[i].Visible 總是會 true。
GridView1.Columns[i].Visible 才能判斷該欄位是否為 true 或 false。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.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"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" 
                SelectCommand="SELECT * FROM [MyTable]"></asp:SqlDataSource>
            <br />
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
                Caption="GridView1:非自動生成列"
                AutoGenerateColumns="False" DataKeyNames="SN" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                    <asp:BoundField DataField="myID" HeaderText="myID" SortExpression="myID" />
                    <asp:BoundField DataField="myName" HeaderText="myName" SortExpression="myName" />
                    <asp:BoundField DataField="purchaseDate" HeaderText="purchaseDate" SortExpression="purchaseDate" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AllowSorting="True" 
                Caption="GridView2:自動生成列"
                AutoGenerateColumns="true" DataKeyNames="SN" DataSourceID="SqlDataSource1">
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
                Caption="GridView3:非自動生成列"
                DataKeyNames="SN" DataSourceID="SqlDataSource1" Visible="false">
                <Columns>
                    <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                    <asp:BoundField DataField="myID" HeaderText="myID" SortExpression="myID" />
                    <asp:BoundField DataField="myName" HeaderText="myName" SortExpression="myName" />
                    <asp:BoundField DataField="purchaseDate" HeaderText="purchaseDate" SortExpression="purchaseDate" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView4" runat="server" AllowPaging="True" AllowSorting="True" 
                Caption="GridView4:非自動生成列"
                AutoGenerateColumns="true" DataKeyNames="SN" DataSourceID="SqlDataSource1" Visible="false">
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView5" runat="server" AllowPaging="True" AllowSorting="True" 
                Caption="GridView5:混合自動生成列"
                AutoGenerateColumns="true" DataKeyNames="SN" DataSourceID="SqlDataSource1" Visible="false">
                <Columns>
                    <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                    <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" Visible="false" />
                    <%--<asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />--%>
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView6" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
                Caption="GridView6:非自動生成列"
                DataKeyNames="SN" DataSourceID="SqlDataSource1" Visible="false">
                <Columns>
                    <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                    <asp:BoundField DataField="myID" HeaderText="myID" SortExpression="myID" Visible="false" />
                    <asp:BoundField DataField="myName" HeaderText="myName" SortExpression="myName" />
                    <asp:BoundField DataField="purchaseDate" HeaderText="purchaseDate" SortExpression="purchaseDate" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="GridView7" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
                Caption="GridView7:非自動生成列"
                DataKeyNames="SN" DataSourceID="SqlDataSource1" Visible="true">
                <Columns>
                    <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                    <asp:BoundField DataField="myID" HeaderText="myID" SortExpression="myID" Visible="false" />
                    <%--<asp:BoundField DataField="myName" HeaderText="myName" SortExpression="myName" />--%>
                    <asp:BoundField DataField="purchaseDate" HeaderText="purchaseDate" SortExpression="purchaseDate" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            <br />
            <br />
            GridView1.Columns.Count 是「非自動生成列數」。不管是否為 Visible。<br />
            GridView1.HeaderRow.Cells.Count 是「自動生成列數」和「非自動生成列數」的總和。不管是否為 Visible。<br />
            <br />
            GridView1.Visible = false; 時,GridView1.HeaderRow.Cells[i].Visible 總是會 false。<br />
            <br />
            <asp:Label ID="Label1" runat="server" ></asp:Label><br />
            <br />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "";
            Label1.Text = Label1.Text + "GridView1.HeaderRow.Cells.Count : " + GridView1.HeaderRow.Cells.Count + "<br />";
            Label1.Text = Label1.Text + "GridView2.HeaderRow.Cells.Count : " + GridView2.HeaderRow.Cells.Count + "<br />";
            Label1.Text = Label1.Text + "GridView3.HeaderRow.Cells.Count : " + GridView3.HeaderRow.Cells.Count + "<br />";
            Label1.Text = Label1.Text + "GridView4.HeaderRow.Cells.Count : " + GridView4.HeaderRow.Cells.Count + "<br />";
            Label1.Text = Label1.Text + "GridView5.HeaderRow.Cells.Count : " + GridView5.HeaderRow.Cells.Count + "<br />";
            Label1.Text = Label1.Text + "GridView6.HeaderRow.Cells.Count : " + GridView6.HeaderRow.Cells.Count + "<br />";
            Label1.Text = Label1.Text + "GridView7.HeaderRow.Cells.Count : " + GridView7.HeaderRow.Cells.Count + "<br />";

            Label1.Text = Label1.Text + "GridView1.Columns.Count : " + GridView1.Columns.Count + "<br />";
            Label1.Text = Label1.Text + "GridView2.Columns.Count : " + GridView2.Columns.Count + "<br />";
            Label1.Text = Label1.Text + "GridView3.Columns.Count : " + GridView3.Columns.Count + "<br />";
            Label1.Text = Label1.Text + "GridView4.Columns.Count : " + GridView4.Columns.Count + "<br />";
            Label1.Text = Label1.Text + "GridView5.Columns.Count : " + GridView5.Columns.Count + "<br />";
            Label1.Text = Label1.Text + "GridView6.Columns.Count : " + GridView6.Columns.Count + "<br />";
            Label1.Text = Label1.Text + "GridView7.Columns.Count : " + GridView7.Columns.Count + "<br />";
        }
    }
}

(完)

沒有留言:

張貼留言