Export (0) Print
Expand All

DataGridColumnCollection Class

A collection of DataGridColumn-derived column objects that represent the columns in a DataGrid control. This class cannot be inherited.

System.Object
  System.Web.UI.WebControls.DataGridColumnCollection

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public sealed class DataGridColumnCollection : ICollection, 
	IEnumerable, IStateManager

The DataGridColumnCollection type exposes the following members.

  NameDescription
Public methodDataGridColumnCollectionInitializes a new instance of the DataGridColumnCollection class.
Top

  NameDescription
Public propertyCountGets the number of columns in the DataGridColumnCollection collection.
Public propertyIsReadOnlyGets a value that indicates whether the columns in the DataGridColumnCollection collection can be modified.
Public propertyIsSynchronizedGets a value indicating whether access to the DataGridColumnCollection collection is synchronized (thread safe).
Public propertyItemGets a DataGridColumn-derived column object from the DataGridColumnCollection collection at the specified index.
Public propertySyncRootGets the object that can be used to synchronize access to the DataGridColumnCollection collection.
Top

  NameDescription
Public methodAddAppends the specified DataGridColumn-derived column object to the end of the DataGridColumnCollection collection.
Public methodAddAtInserts a DataGridColumn-derived column object in the DataGridColumnCollection collection at the specified index.
Public methodClearRemoves all DataGridColumn-derived column objects from the DataGridColumnCollection collection.
Public methodCopyToCopies the items from the DataGridColumnCollection collection to the specified System.Array, starting at the specified index in the System.Array.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns a System.Collections.IEnumerator interface that contains all the DataGridColumn-derived column objects in the DataGridColumnCollection collection.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfReturns the index of the specified DataGridColumn-derived column object from the DataGridColumnCollection collection.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the specified DataGridColumn-derived column object from the DataGridColumnCollection collection.
Public methodRemoveAtRemoves a DataGridColumn-derived column object from the DataGridColumnCollection collection at the specified index.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate propertyIStateManager.IsTrackingViewStateInfrastructure. Gets a value indicating whether the collection is tracking its view-state changes.
Explicit interface implemetationPrivate methodIStateManager.LoadViewStateInfrastructure. Loads the previously saved state.
Explicit interface implemetationPrivate methodIStateManager.SaveViewStateInfrastructure. Returns an object containing state changes.
Explicit interface implemetationPrivate methodIStateManager.TrackViewStateInfrastructure. Starts tracking state changes.
Top

Use the DataGridColumnCollection collection to programmatically manage a collection of DataGridColumn-derived column objects. These objects represent the columns in a DataGrid control. You can add, remove, or insert columns in the DataGridColumnCollection collection.

NoteNote

When the AutoGenerateColumns property is set to true, the columns created by the DataGrid control are not added to the Columns collection.

The DataGrid control does not store the contents of its Columns collection in the view state. To add or remove a column dynamically, you must programmatically add or remove the column every time the page is refreshed. Provide a Page_Init function that adds or removes the column before the DataGrid control's state is reloaded and the control is rebuilt. Otherwise, the changes to the Columns collection are not reflected in the DataGrid control when it is displayed.

NoteNote

Although you can programmatically add columns to or remove columns from the Columns collection of the DataGrid control, it is easier to list the columns statically and then use the Visible property to display or hide each column.

The order of the columns in the collection determines the order that the columns are displayed in the DataGrid control.

The following table lists the different column classes that derive from the DataGridColumn class.

Column Class

Description

BoundColumn

A column that is bound to a field in a data source. It displays each item in the field as text. This is the default column type for the DataGrid control.

ButtonColumn

A column that displays a command button for each item in the column. This allows you to create a column of custom button controls, such as Add or Remove buttons.

EditCommandColumn

A column that contains editing commands for each item in the column.

HyperLinkColumn

A column that displays each item in the column as a hyperlink. The contents of the column can be bound to a field in a data source, or to static text.

TemplateColumn

A column that displays each item in the column according to a specified template. This allows you to control the content of the column, for example to display images.

NoteNote

The DataGridColumn class is the base class for the column classes listed. It is not used directly in the DataGridColumnCollection collection.

The following code example demonstrates how to use the DataGridColumnCollection collection to dynamically add a column to the DataGrid control. Note that the Columns property of the DataGrid control is an instance of the DataGridColumnCollection class.



<%@ 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>



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft