DataGridColumnCollection::AddAt Method (Int32, DataGridColumn^)

 

Inserts a DataGridColumn-derived column object in the DataGridColumnCollection collection at the specified index.

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

public:
void AddAt(
	int index,
	DataGridColumn^ column
)

Parameters

index
Type: System::Int32

The index location in the DataGridColumnCollection at which to insert the DataGridColumn-derived column.

column
Type: System.Web.UI.WebControls::DataGridColumn^

The DataGridColumn-derived column to insert in the DataGridColumnCollection.

Exception Condition
ArgumentNullException

column is null.

Use this method to insert a DataGridColumn-derived column object at the specified index location in the DataGridColumnCollection 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 each time the page is refreshed. Provide a Page_Init function that adds or removes the column before the DataGrid control can reload its state and rebuild itself. Otherwise, the changes to the Columns collection are not reflected in the DataGrid control when it is displayed.

System_CAPS_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 following code example demonstrates how to use the AddAt method to dynamically add a column to the DataGrid control. Note that the Columns property of the DataGrid control is an instance of the DataGridColumnCollection collection.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top
Show: