Example E321
Visible to All Users

Grid View for ASP.NET Web Forms - How to create the control at runtime

This example shows how to create and configure the ASPxGridView control dynamically at runtime.

Follow the steps below to create the control in code.

  1. Call the control type constructor.
  2. Specify the control's ID property.
  3. Attach event handlers.
  4. Specify the control's properties.
  5. Insert the control into the control hierarchy.
  6. Bind the control to data.
C#
protected void Page_Init(object sender, EventArgs e) { ASPxGridView grid = new ASPxGridView(); grid.ID = "grid"; grid.KeyFieldName = "ID"; grid.DataSource = GetData(); this.form1.Controls.Add(grid); grid.DataBind(); }

Files to Review

Default.aspx.cs (VB: Default.aspx.vb)

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Default.aspx.cs(vb)
C#
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using DevExpress.Web; namespace GridRuntime { public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { ASPxGridView grid = new ASPxGridView(); grid.ID = "grid"; grid.KeyFieldName = "ID"; grid.DataSource = GetData(); this.form1.Controls.Add(grid); grid.DataBind(); } public DataTable GetData() { DataTable Table = new DataTable(); Table.Columns.Add("ID", typeof(int)); Table.Columns.Add("ItemName", typeof(string)); Table.Rows.Add(1, "A"); Table.Rows.Add(2, "B"); return Table; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.