語言

DataGridColumnCollection 類別

定義

一組 DataGridColumn代表控制項中 DataGrid 欄位的 -衍生欄位物件。 此類別無法獲得繼承。

public ref class DataGridColumnCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class DataGridColumnCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type DataGridColumnCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class DataGridColumnCollection
Implements ICollection, IStateManager
繼承
DataGridColumnCollection
實作

範例

以下程式碼範例示範如何利用 DataGridColumnCollection 集合動態新增欄位到控制項 DataGrid 中。 請注意,ColumnsDataGrid控制項的性質是該DataGridColumnCollection類別的實例。


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script runat="server">

      ICollection CreateDataSource() 
      {
      
         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;

      }
 
      void Page_Load(Object sender, EventArgs e) 
      {

         // Create a DataGrid control.
         DataGrid ItemsGrid = new DataGrid();

         // Set the properties of the DataGrid.
         ItemsGrid.ID = "ItemsGrid";
         ItemsGrid.BorderColor = System.Drawing.Color.Black;
         ItemsGrid.CellPadding = 3;
         ItemsGrid.AutoGenerateColumns = false;

         // Set the styles for the DataGrid.
         ItemsGrid.HeaderStyle.BackColor = 
             System.Drawing.Color.FromArgb(0x0000aaaa);

         // Create the columns for the DataGrid control. The DataGrid
         // columns are dynamically generated. Therefore, the columns   
         // must be re-created each time the page is refreshed.
         
         // Create and add the columns to the collection.
         ItemsGrid.Columns.Add(CreateBoundColumn("IntegerValue", "Item"));
         ItemsGrid.Columns.Add(
             CreateBoundColumn("StringValue", "Description"));
         ItemsGrid.Columns.Add(
             CreateBoundColumn("CurrencyValue", "Price", "{0:c}", 
             HorizontalAlign.Right));
         ItemsGrid.Columns.Add(
             CreateLinkColumn("http://www.microsoft.com", "_self", 
             "Microsoft", "Related link"));
        
         // Specify the data source and bind it to the control.
         ItemsGrid.DataSource = CreateDataSource();
         ItemsGrid.DataBind();

         // Add the DataGrid control to the Controls collection of 
         // the PlaceHolder control.
         Place.Controls.Add(ItemsGrid);

      }

      BoundColumn CreateBoundColumn(String DataFieldValue, 
          String HeaderTextValue)
      {

         // This version of the CreateBoundColumn method sets only the
         // DataField and HeaderText properties.

         // Create a BoundColumn.
         BoundColumn column = new BoundColumn();

         // Set the properties of the BoundColumn.
         column.DataField = DataFieldValue;
         column.HeaderText = HeaderTextValue;

         return column;

      }

      BoundColumn CreateBoundColumn(String DataFieldValue, 
          String HeaderTextValue, String FormatValue, 
          HorizontalAlign AlignValue)
      {

         // This version of CreateBoundColumn method sets the DataField,
         // HeaderText, and DataFormatString properties. It also sets the 
         // HorizontalAlign property of the ItemStyle property of the column. 

         // Create a BoundColumn using the overloaded CreateBoundColumn method.
         BoundColumn column = CreateBoundColumn(DataFieldValue, HeaderTextValue);

         // Set the properties of the BoundColumn.
         column.DataFormatString = FormatValue;
         column.ItemStyle.HorizontalAlign = AlignValue;

         return column;

      }

      HyperLinkColumn CreateLinkColumn(String NavUrlValue, 
          String TargetValue, String TextValue, String HeaderTextValue)
      {

         // Create a BoundColumn.
         HyperLinkColumn column = new HyperLinkColumn();

         // Set the properties of the ButtonColumn.
         column.NavigateUrl = NavUrlValue;
         column.Target = TargetValue;
         column.Text = TextValue;
         column.HeaderText = HeaderTextValue;

         return column;

      }

   </script>
 
<head runat="server">
    <title>DataGrid Constructor Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid Constructor Example</h3>
 
      <b>Product List</b>

      <asp:PlaceHolder id="Place"
           runat="server"/>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!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" >
   <script runat="server">

      Function CreateDataSource() As ICollection 
      
         ' Create sample data for the DataGrid control.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow
 
         ' Define the columns of the table.
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(string)))
         dt.Columns.Add(New DataColumn("CurrencyValue", GetType(double)))
 
         ' Populate the table with sample values.
         Dim i As Integer

         For i = 0 to 8 
        
            dr = dt.NewRow()
 
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 * (i + 1)
 
            dt.Rows.Add(dr)

         Next i
 
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function
 
      Sub Page_Load(sender As Object, e As EventArgs) 

         ' Create a DataGrid control.
         Dim ItemsGrid As DataGrid = New DataGrid()

         ' Set the properties of the DataGrid.
         ItemsGrid.ID = "ItemsGrid"
         ItemsGrid.BorderColor = System.Drawing.Color.Black
         ItemsGrid.CellPadding = 3
         ItemsGrid.AutoGenerateColumns = False

         ' Set the styles for the DataGrid.
         ItemsGrid.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(&H0000aaaa)

         ' Create the columns for the DataGrid control. The DataGrid
         ' columns are dynamically generated. Therefore, the columns   
         ' must be re-created each time the page is refreshed.
         
         ' Create and add the columns to the collection.
         ItemsGrid.Columns.Add(CreateBoundColumn("IntegerValue", "Item"))
         ItemsGrid.Columns.Add( _
             CreateBoundColumn("StringValue", "Description"))
         ItemsGrid.Columns.Add( _
             CreateBoundColumn("CurrencyValue", "Price", "{0:c}", _
             HorizontalAlign.Right))
         ItemsGrid.Columns.Add( _
             CreateLinkColumn("http:'www.microsoft.com", "_self", _
             "Microsoft", "Related link"))
        
         ' Specify the data source and bind it to the control.     
         ItemsGrid.DataSource = CreateDataSource()
         ItemsGrid.DataBind()

         ' Add the DataGrid control to the Controls collection of 
         ' the PlaceHolder control.
         Place.Controls.Add(ItemsGrid)

      End Sub

      Function CreateBoundColumn(DataFieldValue As String, HeaderTextValue As String) As BoundColumn

         ' This version of CreateBoundColumn method sets only the 
         ' DataField and HeaderText properties.

         ' Create a BoundColumn.
         Dim column As BoundColumn = New BoundColumn()

         ' Set the properties of the BoundColumn.
         column.DataField = DataFieldValue
         column.HeaderText = HeaderTextValue

         Return column

      End Function

      Function CreateBoundColumn(DataFieldValue As String, _
          HeaderTextValue As String, FormatValue As String, _
          AlignValue As HorizontalAlign) As BoundColumn

         ' This version of CreateBoundColumn method sets the DataField,
         ' HeaderText, and DataFormatString properties. It also sets the 
         ' HorizontalAlign property of the ItemStyle property of the column. 

         ' Create a BoundColumn using the overloaded CreateBoundColumn method.
         Dim column As BoundColumn = CreateBoundColumn(DataFieldValue, HeaderTextValue)

         ' Set the properties of the BoundColumn.
         column.DataFormatString = FormatValue
         column.ItemStyle.HorizontalAlign = AlignValue

         Return column

      End Function

      Function CreateLinkColumn(NavUrlValue As String, TargetValue As String, _
         TextValue As String, HeaderTextValue As String) As HyperLinkColumn 

         ' Create a BoundColumn.
         Dim column As HyperLinkColumn = New HyperLinkColumn()

         ' Set the properties of the ButtonColumn.
         column.NavigateUrl = NavUrlValue
         column.Target = TargetValue
         column.Text = TextValue
         column.HeaderText = HeaderTextValue

         Return column

      End Function

   </script>
 
<head runat="server">
    <title>DataGrid Constructor Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid Constructor Example</h3>
 
      <b>Product List</b>

      <asp:PlaceHolder id="Place"
           runat="server"/>
 
   </form>
 
</body>
</html>

備註

使用該 DataGridColumnCollection 集合來程式化管理一組 DataGridColumn-衍生的欄位物件集合。 這些物件代表控制項中的 DataGrid 欄位。 你可以在收藏中新增、移除或插入欄位 DataGridColumnCollection 。

Note

當屬性 AutoGenerateColumns 設定為 時 true, ,控制項所建立 DataGrid 的欄位不會被 Columns 加入集合。

DataGrid控制項不會以檢視狀態儲存其Columns集合的內容。 要動態新增或移除欄位,必須在每次頁面刷新時以程式方式新增或移除欄位。 提供 Page_Init 一個函式,在重新載入控制項狀態並重建控制項前,先新增或移除欄位 DataGrid 。 否則,當顯示時,收藏的變更 Columns 不會反映在控制中 DataGrid 。

Note

雖然你可以程式化地將欄位加入或移除控制項集合Columns中的DataGrid欄位,但靜態列出欄位並利用Visible屬性顯示或隱藏每一欄會更為簡單。

集合中欄位的順序決定了欄位在控制項 DataGrid 中顯示的順序。

下表列出由該 DataGridColumn 類別衍生的不同欄位類別。

柱級 Description
BoundColumn 一個綁定到資料來源欄位的欄位。 它會以文字形式顯示欄位中的每一項。 這是控制項的 DataGrid 預設欄位類型。
ButtonColumn 一個欄位顯示每個欄位中每個項目的指令按鈕。 這允許你建立一欄自訂按鈕控制,例如新增或移除按鈕。
EditCommandColumn 一欄包含每個欄位中每個項目的編輯指令。
HyperLinkColumn 一個欄位,將欄位中的每個項目以超連結的形式顯示。 欄位的內容可以綁定到資料來源中的欄位,或靜態文字。
TemplateColumn 一個欄位,依照指定範本顯示欄位中的每個項目。 這讓你能控制欄位內容,例如顯示圖片。

Note

該 DataGridColumn 類別是上述欄位類別的基底類別。 它並未直接用於收藏 DataGridColumnCollection 中。

建構函式

名稱 Description
DataGridColumnCollection(DataGrid, ArrayList)

初始化 DataGridColumnCollection 類別的新執行個體。

屬性

名稱 Description
Count

取得集合中的 DataGridColumnCollection 欄位數量。

IsReadOnly

會取得一個值,表示集合中的 DataGridColumnCollection 欄位是否可以被修改。

IsSynchronized

會獲得一個值,表示存取 DataGridColumnCollection 集合是否同步(執行緒安全)。

Item[Int32]

從指定索引的DataGridColumn集合中取得 DataGridColumnCollection-衍生的欄位物件。

SyncRoot

取得可用來同步存取 DataGridColumnCollection 集合的物件。

方法

名稱 Description
Add(DataGridColumn)

將指定的 DataGridColumn-衍生欄位物件附加到集合的 DataGridColumnCollection 末尾。

AddAt(Int32, DataGridColumn)

在指定的索引處插入 DataGridColumn一個 -衍生的欄位物件 DataGridColumnCollection 。

Clear()

移除集合中DataGridColumn所有 DataGridColumnCollection-衍生欄位物件。

CopyTo(Array, Int32)

從 DataGridColumnCollection 集合中複製項目到指定的 Array索引 Array。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

回傳IEnumerator一個介面,包含集合中DataGridColumn所有 DataGridColumnCollection-衍生欄位物件。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
IndexOf(DataGridColumn)

回傳集合DataGridColumn中指定 DataGridColumnCollection-衍生欄位物件的索引。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Remove(DataGridColumn)

從集合中移除指定的 DataGridColumn-衍生欄位物件 DataGridColumnCollection 。

RemoveAt(Int32)

在指定索引處移除集合中導 DataGridColumn出的 -欄位物件 DataGridColumnCollection 。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
IStateManager.IsTrackingViewState

會取得一個值,表示集合是否正在追蹤其檢視狀態的變化。

IStateManager.LoadViewState(Object)

載入先前儲存的狀態。

IStateManager.SaveViewState()

回傳包含狀態變更的物件。

IStateManager.TrackViewState()

開始追蹤狀態變化。

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

將 IEnumerable 轉換成 IQueryable。

Cast<TResult>(IEnumerable)

將 IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於

另請參閱