DataGridColumnCollection.AddAt Method
Inserts a DataGridColumn derived column object in the DataGridColumnCollection at the specified index.
[Visual Basic] Public Sub AddAt( _ ByVal index As Integer, _ ByVal column As DataGridColumn _ ) [C#] public void AddAt( int index, DataGridColumn column ); [C++] public: void AddAt( int index, DataGridColumn* column ); [JScript] public function AddAt( index : int, column : DataGridColumn );
Parameters
- index
- The index location in the DataGridColumnCollection to insert the DataGridColumn derived column object.
- column
- The DataGridColumn derived column object to insert into the DataGridColumnCollection.
Remarks
Use this method to insert a DataGridColumn derived column object at the specified index location in the 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 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.
Note 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.
Example
[Visual Basic, C#] The following example demonstrates how 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.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <script language="VB" runat="server"> Dim Cart As DataTable Dim CartView As DataView Function CreateDataSource() As ICollection Dim dt As New DataTable() Dim dr As DataRow dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32))) dt.Columns.Add(New DataColumn("StringValue", GetType(String))) dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double))) Dim i As Integer For i = 0 To 8 dr = dt.NewRow() dr(0) = i dr(1) = "Item " & i.ToString() dr(2) = 1.23 *(i + 1) dt.Rows.Add(dr) Next i Dim dv As New DataView(dt) Return dv End Function 'CreateDataSource Sub Page_Load(sender As Object, e As EventArgs) If Session("DG4_ShoppingCart") Is Nothing Then Cart = New DataTable() Cart.Columns.Add(New DataColumn("Item", GetType(String))) Cart.Columns.Add(New DataColumn("Price", GetType(String))) Session("DG4_ShoppingCart") = Cart Else Cart = CType(Session("DG4_ShoppingCart"), DataTable) End If CartView = New DataView(Cart) ShoppingCart.DataSource = CartView ShoppingCart.DataBind() If Not IsPostBack Then ' Load this data only once. ItemsGrid.DataSource = CreateDataSource() ItemsGrid.DataBind() End If End Sub 'Page_Load Sub Page_Init(sender As Object, e As EventArgs) ' Create a dynamic column to add to Columns collection. Dim NumberColumn As New BoundColumn() NumberColumn.HeaderText = "Item Number" NumberColumn.DataField = "IntegerValue" ' Add column to Columns collection. ItemsGrid.Columns.AddAt(2, NumberColumn) End Sub 'Page_Init Sub Grid_CartCommand(sender As Object, e As DataGridCommandEventArgs) Dim dr As DataRow = Cart.NewRow() ' e.Item is the table row where the command is raised. ' For bound columns, the value is stored in the Text property of the TableCell. Dim itemCell As TableCell = e.Item.Cells(2) Dim priceCell As TableCell = e.Item.Cells(3) Dim item As String = itemCell.Text Dim price As String = priceCell.Text If CType(e.CommandSource, Button).CommandName = "AddToCart" Then dr(0) = item dr(1) = price Cart.Rows.Add(dr) Else 'Remove from Cart. CartView.RowFilter = "Item='" & item & "'" If CartView.Count > 0 Then CartView.Delete(0) End If CartView.RowFilter = "" End If ShoppingCart.DataBind() End Sub 'Grid_CartCommand </script> <body> <form runat=server> <h3>DataGridColumnCollection AddAt Example</h3> <table cellpadding="5"> <tr valign="top"> <td> <b>Product List</b> <asp:DataGrid id="ItemsGrid" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateColumns="false" OnItemCommand="Grid_CartCommand" runat="server"> <HeaderStyle BackColor="#00aaaa"> </HeaderStyle> <Columns> <asp:ButtonColumn HeaderText="Add to cart" ButtonType="PushButton" Text="Add" CommandName="AddToCart"/> <asp:ButtonColumn HeaderText="Remove from cart" ButtonType="PushButton" Text="Remove" CommandName="RemoveFromCart"/> <asp:BoundColumn HeaderText="Item" DataField="StringValue"/> <asp:BoundColumn HeaderText="Price" DataField="CurrencyValue" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="right"> </ItemStyle> </asp:BoundColumn> </Columns> </asp:DataGrid> </td> <td> <b>Shopping Cart</b> <asp:DataGrid id="ShoppingCart" runat="server" BorderColor="black" BorderWidth="1" GridLines="Both" ShowFooter="false" CellPadding="3" CellSpacing="0"> <HeaderStyle BackColor="#00aaaa"> </HeaderStyle> </asp:DataGrid> </td> </tr> </table> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <%@ Import Namespace="System.Data" %> <html> <script language="C#" runat="server"> DataTable Cart; DataView CartView; ICollection CreateDataSource() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32))); dt.Columns.Add(new DataColumn("StringValue", typeof(string))); dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double))); 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) { if (Session["DG4_ShoppingCart"] == null) { Cart = new DataTable(); Cart.Columns.Add(new DataColumn("Item", typeof(string))); Cart.Columns.Add(new DataColumn("Price", typeof(string))); Session["DG4_ShoppingCart"] = Cart; } else { Cart = (DataTable)Session["DG4_ShoppingCart"]; } CartView = new DataView(Cart); ShoppingCart.DataSource = CartView; ShoppingCart.DataBind(); if (!IsPostBack) { // Load this data only once. ItemsGrid.DataSource= CreateDataSource(); ItemsGrid.DataBind(); } } void Page_Init(Object sender, EventArgs e) { // Create a dynamic column to add to Columns collection. BoundColumn NumberColumn = new BoundColumn(); NumberColumn.HeaderText="Item Number"; NumberColumn.DataField="IntegerValue"; // Add column to Columns collection. ItemsGrid.Columns.AddAt(2, NumberColumn); } void Grid_CartCommand(Object sender, DataGridCommandEventArgs e) { DataRow dr = Cart.NewRow(); // e.Item is the table row where the command is raised. // For bound columns, the value is stored in the Text property of the TableCell. TableCell itemCell = e.Item.Cells[2]; TableCell priceCell = e.Item.Cells[3]; string item = itemCell.Text; string price = priceCell.Text; if (((Button)e.CommandSource).CommandName == "AddToCart") { dr[0] = item; dr[1] = price; Cart.Rows.Add(dr); } else { //Remove from Cart. CartView.RowFilter = "Item='" + item + "'"; if (CartView.Count > 0) { CartView.Delete(0); } CartView.RowFilter = ""; } ShoppingCart.DataBind(); } </script> <body> <form runat=server> <h3>DataGridColumnCollection AddAt Example</h3> <table cellpadding="5"> <tr valign="top"> <td> <b>Product List</b> <asp:DataGrid id="ItemsGrid" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateColumns="false" OnItemCommand="Grid_CartCommand" runat="server"> <HeaderStyle BackColor="#00aaaa"> </HeaderStyle> <Columns> <asp:ButtonColumn HeaderText="Add to cart" ButtonType="PushButton" Text="Add" CommandName="AddToCart"/> <asp:ButtonColumn HeaderText="Remove from cart" ButtonType="PushButton" Text="Remove" CommandName="RemoveFromCart"/> <asp:BoundColumn HeaderText="Item" DataField="StringValue"/> <asp:BoundColumn HeaderText="Price" DataField="CurrencyValue" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="right"> </ItemStyle> </asp:BoundColumn> </Columns> </asp:DataGrid> </td> <td> <b>Shopping Cart</b> <asp:DataGrid id="ShoppingCart" runat="server" BorderColor="black" BorderWidth="1" GridLines="Both" ShowFooter="false" CellPadding="3" CellSpacing="0"> <HeaderStyle BackColor="#00aaaa"> </HeaderStyle> </asp:DataGrid> </td> </tr> </table> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
DataGridColumnCollection Class | DataGridColumnCollection Members | System.Web.UI.WebControls Namespace | DataGrid | Columns | DataGridColumn | Add | Remove | RemoveAt | Clear