This example shows how to create and configure the ASPxGridView control dynamically at runtime.
Follow the steps below to create the control in code.
- Call the control type constructor.
- Specify the control's
ID
property. - Attach event handlers.
- Specify the control's properties.
- Insert the control into the control hierarchy.
- 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
- How to create and configure a HyperLink column at runtime
- How to create a DataItemTemplate for a column at Runtime
- How to create a master-detail grid at runtime
- How to dynamically switch the Grid's data source and recreate columns at runtime
- How to create columns and bind the control to different data sources at runtime
- How to bind the GridViewDataComboBoxColumn edit form editor at runtime
- How to bind the control created in design mode to different data sources at runtime
- How to bind a grid created at runtime to different data sources
- How to edit a DataTable stored in ViewState at runtime
- How to edit data in a DataTable at runtime when data is stored in Session
- How to load UserControl within EditFormTemplate at runtime
- How to switch date formats for the GridViewDataDateColumn at runtime
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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;
}
}
}