Export (0) Print
Expand All

DataControlFieldCollection.Add Method

Appends the specified DataControlField object to the end of the collection.

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

public void Add(
	DataControlField field
)

Parameters

field
Type: System.Web.UI.WebControls.DataControlField

The DataControlField to append to the collection.

Use the Add method to append a DataControlField object to the end of the collection. This implementation of the method takes the DataControlField object specified by the field parameter and appends it to the collection.

The following code example demonstrates how to use the Add method to dynamically add a CheckBoxField object to a GridView control.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // The columns need to be created only the first time 
    // the page is loaded. 
    if(!IsPostBack)
    {

      // Dynamically create columns to display the desired 
      // fields from the data source. Columns that are  
      // dynamically added to the GridView control are not persisted  
      // across posts and must be recreated each time the page is  
      // loaded. 

      // Create a BoundField object to display an author's last name.
      BoundField lastNameBoundField = new BoundField();
      lastNameBoundField.DataField = "au_lname";
      lastNameBoundField.HeaderText = "Last Name";

      // Create a CheckBoxField object to indicate whether the author 
      // is on contract.
      CheckBoxField contractCheckBoxField = new CheckBoxField();
      contractCheckBoxField.DataField = "contract";
      contractCheckBoxField.HeaderText = "Contract";

      // Add the columns to the Columns collection of the 
      // GridView control.
      AuthorsGridView.Columns.Add(lastNameBoundField);
      AuthorsGridView.Columns.Add(contractCheckBoxField);

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>CheckBoxField Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>CheckBoxField Constructor Example</h3>

      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">                
      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft