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 加入 控制項。 請注意, Columns 控制項的 DataGrid 屬性是 類別 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 新增、移除或插入資料行。

注意

AutoGenerateColumns當 屬性設定為 true, 控制項所建立的資料 DataGrid 行時,不會加入 Columns 集合中。

控制項 DataGrid 不會將其集合的內容 Columns 儲存在檢視狀態中。 若要動態新增或移除資料行,您必須在每次重新整理頁面時,以程式設計方式新增或移除資料行。 提供在 Page_Init 重載控制項狀態並重建控制項之前 DataGrid 新增或移除資料行的函式。 否則,當集合顯示時,集合的 Columns 變更不會反映在 DataGrid 控制項中。

注意

雖然您可以程式設計方式在 控制項的 DataGrid 集合中加入或移除 Columns 資料行,但您可以更輕鬆地以靜態方式列出資料行,然後使用 Visible 屬性來顯示或隱藏每個資料行。

集合中資料行的順序會決定資料行在 控制項中顯示的 DataGrid 順序。

下表列出衍生自 DataGridColumn 類別的不同資料行類別。

資料行類別 描述
BoundColumn 系結至資料來源中欄位的資料行。 它會將欄位中的每個專案顯示為文字。 這是控制項的預設資料行類型 DataGrid
ButtonColumn 顯示資料行中每個專案的命令按鈕的資料行。 這可讓您建立自訂按鈕控制項的資料行,例如 [新增] 或 [移除] 按鈕。
EditCommandColumn 包含資料行中每個專案的編輯命令的資料行。
HyperLinkColumn 顯示資料行中每個專案做為超連結的資料行。 資料行的內容可以系結至資料來源中的欄位,或系結至靜態文字。
TemplateColumn 根據指定的範本,顯示資料行中每個專案的資料行。 這可讓您控制資料行的內容,例如顯示影像。

注意

類別 DataGridColumn 是列出的資料行類別的基類。 它不會直接在集合中使用 DataGridColumnCollection

建構函式

DataGridColumnCollection(DataGrid, ArrayList)

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

屬性

Count

取得 DataGridColumnCollection 集合中的資料行數目。

IsReadOnly

取得值,指出是否可以修改 DataGridColumnCollection 集合中的資料行。

IsSynchronized

取得值,指出對 DataGridColumnCollection 集合的存取是否為同步 (具備執行緒安全)。

Item[Int32]

DataGridColumn 集合中指定的索引處,取得 DataGridColumnCollection 衍生資料行物件。

SyncRoot

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

方法

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)

明確介面實作

IStateManager.IsTrackingViewState

取得值,指出集合是否正在追蹤它的檢視狀態變更。

IStateManager.LoadViewState(Object)

載入先前儲存的狀態。

IStateManager.SaveViewState()

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

IStateManager.TrackViewState()

啟動追蹤狀態的變更。

擴充方法

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱