System.Web.UI.WebControls Namespace DataGridColumnCollection Class
Appends a DataGridColumn to the collection.
[ VB ]
Public Sub Add ( _
ByVal column As DataGridColumn _
)
[ C# ]
public void Add (
DataGridColumn column
);
[ C++ ]
public: void Add (
DataGridColumn* column
);
[ JScript ]
public function Add (
column : DataGridColumn
);
- column
- The DataGridColumn to append to the collection.
Use this method to add a DataGridColumn derived object to the end of a DataGridColumnCollection.
The DataGrid 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 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.
NOTE: While 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 example demonstrates how use the Add method to dynamically add a column to the DataGrid control.
void Page_Init ( Object src, EventArgs e ) {
// create a dynamic column to add to Columns collection.
BoundColumn newColumn = new BoundColumn ( );
newColumn.HeaderText = "Item Number";
newColumn.DataField = "ProductID";
// add column to Columns collection.
myGrid.Columns.Add ( newColumn );
}
Sub Page_Init ( src As Object, e As EventArgs )
' create a dynamic column to add to Columns collection.
Dim newColumn As New BoundColumn ( )
newColumn.HeaderText = "Item Number"
newColumn.DataField = "ProductID"
' add column to Columns collection.
myGrid.Columns.Add ( newColumn )
End Sub |
|
C# |
VB |
DataGridColumnCollection Members AddAt Remove RemoveAt