asp.net.ph

DataControlFieldCollection.Add Method

System.Web.UI.WebControls Namespace   DataControlFieldCollection Class


.NET Framework version 2.0

Appends a DataControlField to the collection.

[ VB ]
Public Sub Add ( _
   ByVal column As DataControlField _
)

[ C# ]
public void Add (
   DataControlField column
);

[ C++ ]
public: void Add (
   DataControlField* column
);

[ JScript ]
public function Add (
   column : DataControlField
);

Parameters

column
The DataControlField to append to the collection.

Remarks

Use this method to add a DataControlField derived object to the end of a DataControlFieldCollection.

The GridView control does not store the contents of its Columns collection into the view state. To add or remove a column dynamically, you must programmaticallt add or remove the column each time the page is refreshed. Provide a Page_Init function that adds or removes the column before the GridView control can reload its state and rebuild itself. Otherwise, the changes to the Columns collection are not reflected in the GridView control when it is displayed.

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

Example

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

void Page_Init ( Object src, EventArgs e ) {
   // create a dynamic column to add to Columns collection.
   BoundField newField = new BoundField ( );
   newField.HeaderText = "Item Number"; 
   newField.DataField = "ProductID";

   // add column to Columns collection.
   myGrid.Columns.Add ( newField );
}
  C# VB

See Also

DataControlFieldCollection Members   Remove   RemoveAt Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph