DataGridColumnCollection 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
DataGridColumn 派生的列对象的集合,这些对象表示 DataGrid 控件中的列。 此类不能被继承。
public ref class DataGridColumnCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManagerpublic sealed class DataGridColumnCollection : System.Collections.ICollection, System.Web.UI.IStateManagertype DataGridColumnCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManagerPublic 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 显示顺序。
下表列出了派生自 DataGridColumn 类的不同列类。
| Column 类 | 说明 | 
|---|---|
| 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。 |