语言

DataGridColumnCollection 类

定义

表示控件中的DataGridColumn列的 DataGrid-derived 列对象的集合。 此类不能被继承。

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 集合以编程方式管理 -derived 列对象的集合 DataGridColumn。 这些对象表示控件中的 DataGrid 列。 可以在集合中添加 DataGridColumnCollection 、删除或插入列。

注释

当属性AutoGenerateColumns设置为控件创建的true,列时,不会添加到集合中DataGridColumns。

控件 DataGrid 不将其 Columns 集合的内容存储在视图状态中。 若要动态添加或删除列,必须在每次刷新页面时以编程方式添加或删除列。 提供在 Page_Init 重新加载控件状态并重新生成控件之前 DataGrid 添加或删除列的函数。 否则,显示集合时,集合的更改 Columns 不会反映在控件中 DataGrid 。

注释

虽然可以通过编程方式向控件集合Columns中添加列或删除列DataGrid,但以静态方式列出列更容易,然后使用Visible属性显示或隐藏每列。

集合中列的顺序决定了列在控件中显示的 DataGrid 顺序。

下表列出了派生自 DataGridColumn 该类的不同列类。

Column 类 Description
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接口,该接口包含集合中的所有DataGridColumnDataGridColumnCollection派生列对象。

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()

开始跟踪状态更改。

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

将 IEnumerable 转换为 IQueryable。

Cast<TResult>(IEnumerable)

将 IEnumerable 的元素强制转换为指定类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于

另请参阅