GridView.Columns 屬性

定義

取得 DataControlField 物件的集合,其表示 GridView 控制項中的資料行欄位。

public:
 virtual property System::Web::UI::WebControls::DataControlFieldCollection ^ Columns { System::Web::UI::WebControls::DataControlFieldCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.DataControlFieldCollection Columns { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.Columns : System.Web.UI.WebControls.DataControlFieldCollection
Public Overridable ReadOnly Property Columns As DataControlFieldCollection

屬性值

DataControlFieldCollection,包含 GridView 控制項中的所有資料行欄位。

屬性

範例

下列範例示範如何以宣告方式填入 Columns 集合。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Columns Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView Columns Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        runat="server">
        
        <columns>
          <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName" headertext="Company Name"/>
          <asp:boundfield datafield="Address" headertext="Address"/>
          <asp:boundfield datafield="City" headertext="City"/>
          <asp:boundfield datafield="PostalCode" headertext="Postal Code"/>
          <asp:boundfield datafield="Country" headertext="Country"/>
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Columns Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView Columns Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        runat="server">
        
        <columns>
          <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName" headertext="Company Name"/>
          <asp:boundfield datafield="Address" headertext="Address"/>
          <asp:boundfield datafield="City" headertext="City"/>
          <asp:boundfield datafield="PostalCode" headertext="Postal Code"/>
          <asp:boundfield datafield="Country" headertext="Country"/>
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

備註

資料列欄位代表 控制項中的資料 GridView 行。 屬性 Columns (集合) 用來儲存控制項中 GridView 呈現的所有明確宣告資料列欄位。 您也可以使用 Columns 集合,以程式設計方式管理資料列欄位的集合。

注意

明確宣告的資料列欄位可以與自動產生的資料列欄位搭配使用。 同時使用這兩個欄位時,會先轉譯明確宣告的資料列欄位,後面接著自動產生的資料列欄位。 自動產生的資料列欄位不會新增至 Columns 集合。

資料列欄位會依資料列欄位出現在集合中的順序,顯示在 GridView 控制項中 Columns 。 下表顯示衍生自 DataControlField 類別的不同資料列欄位類別,並可用於 Columns 集合中。

資料列欄位類型 描述
BoundField 顯示資料來源中欄位的值。 這是控制項的預設資料行類型 GridView
ButtonField 顯示 控制項中 GridView 每個專案的命令按鈕。 這可讓您建立自訂按鈕控制項的資料行,例如 [新增] 或 [移除] 按鈕。
CheckBoxField 顯示 控制項中 GridView 每個專案的核取方塊。 此資料列欄位類型通常用來顯示具有布林值的欄位。
CommandField 顯示預先定義的命令按鈕,以執行選取、編輯或刪除作業。
HyperLinkField 將資料來源中的欄位值顯示為超連結。 此資料列欄位類型可讓您將第二個欄位系結至超連結的 URL。
ImageField 顯示 控制項中 GridView 每個專案的影像。
TemplateField 根據指定的範本,顯示控制項中 GridView 每個專案的使用者定義內容。 此資料列欄位類型可讓您建立自訂資料列欄位。

雖然您可以以程式設計方式將資料列欄位 Columns 加入集合中,但更容易在 控制項中 GridView 以宣告方式列出資料列欄位,然後使用 Visible 每個資料列欄位的 屬性來顯示或隱藏每個資料列欄位。

如果資料 Visible 列欄位的 屬性設定 false 為 ,則資料行不會顯示在 控制項中 GridView ,而且資料行的資料不會進行往返用戶端。 如果您想要不可見的資料行進行往返,請將功能變數名稱新增至 DataKeyNames 屬性。

適用於

另請參閱