WebGrid provides events in Server-Side to enable user implementing several tasks.
In this topic, you will learn how to create WebGrid in Server-Side programmatically.
To create WebGrid in Server-Side programmatically
- Put the following code in Page_Load event. These codes are used to define WebGrid's settings and events.
C# Copy Code protected void Page_Load(object sender, EventArgs e) { WebGrid grid = new WebGrid(); grid.ID = "WebGrid1"; grid.Width = System.Web.UI.WebControls.Unit.Pixel(800); grid.Height = System.Web.UI.WebControls.Unit.Pixel(600); grid.UseDefaultStyle = true; grid.InitializeDataSource += new DataSourceEventHandler(grid_InitializeDataSource); grid.PrepareDataBinding += new DataSourceEventHandler(grid_PrepareDataBinding); grid.InitializeLayout += new LayoutEventHandler(grid_InitializeLayout); form1.Controls.Add(grid); }
- Put the following code in OnInit event.
C# Copy Code protected override void OnInit(EventArgs e) { base.OnInit(e); }
- Put the following code in InitializeDataSource Server-Side event of WebGrid. These codes are used to bind the Customers table to DataSource.
C# Copy Code protected void grid_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { dsNorthwind.CustomersDataTable dt = new dsNorthwind.CustomersDataTable(); dsNorthwindTableAdapters.CustomersTableAdapter da = new dsNorthwindTableAdapters.CustomersTableAdapter(); da.Fill(dt); e.DataSource = dt; }
- Put the following code in PrepareDataBinding Server-Side event of WebGrid. These codes are used to retrieve and display the table structure in WebGrid.
C# Copy Code private void grid_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { if (!IsPostBack) { WebGrid grid = (WebGrid)sender; grid.RetrieveStructure(); } }
- Put the following code in InitializeLayout Server-Side event of WebGrid. These codes are used to set the layout settings available in WebGrid. In this case, WebGrid is set to enable context menu, export and edit.
C# Copy Code protected void grid_InitializeLayout(object sender, ISNet.WebUI.WebGrid.LayoutEventArgs e) { e.Layout.AllowContextMenu = true; e.Layout.AllowExport = Export.Yes; e.Layout.AllowEdit = Edit.Yes; }
- Run the project.
Other Resources
Walkthrough Topics
How-to Topics