This walkthrough shows you how to configure updatable WebGrid bound to DataSet.
During this walkthrough, you will learn how to do the following:
- Use DataSet.
- Use SmartTag to allow Add New, AllowEdit and Allow Delete.
Prerequisites
In order to complete this walkthrough, you will need the following:
- Access to the Microsoft Access Northwind database.
- Visual Studio 2008 Application.
Step-By-Step Instructions
To create new web application and bind WebGrid to AccessDataSource
- Launch Visual Studio.NET 2008.
- Click on File menu, then select New and click Web Site.
- Select ASP.NET Web Site in the Template box and set Location to HTTP.
- Named the Web Site and click OK.
- Right-click on Project's name and select Add New Item.
- Select Intersoft AppForm in the My Templates box and named it as Walkthrough.aspx.
- Drag WebGrid instance from ToolBar to WebForm.
- In the Solution Explorer, right-click on App_Data and select Add Existing Item.
- Browse and add NorthWind.mdb in C:\Program Files\Intersoft Solutions\Data (Default installation folder).
- In the Solution Explorer, Add App_Code folder on your project and create new DataSet that connect to NorthWind.mdb in that folder.
- Click the SmartTag on the upper right of the WebGrid.
- Select Allow Add New, Allow Editing, and Allow Delete properties.
- Set AllowBatchUpdate to True on WebGrid Layout Settings.
- Add InitializeDataSource, PrepareDataBinding, and BatchUpdate events from toolbar properties:
- On InitializeDataSource event add these codes to bind WebGrid into Dataset:
C# Copy Code protected void WebGrid1_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; }
- On PrepareDataBinding event add these codes to retrieve WebGrid structure:
C# Copy Code protected void WebGrid1_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { if (!IsPostBack) WebGrid1.RetrieveStructure(); }
- BatchUpdate event add these codes to update all changes that user made during batch update:
C# Copy Code protected void WebGrid1_BatchUpdate(object sender, ISNet.WebUI.WebGrid.BatchUpdateEventArgs e) { DsNorthwindTableAdapters.CustomersTableAdapter da = new DsNorthwindTableAdapters.CustomersTableAdapter(); DsNorthwind.CustomersDataTable dtCust = (DsNorthwind.CustomersDataTable)WebGrid1.GetCachedDataSource(); DsNorthwind.CustomersDataTable changeDT = (DsNorthwind.CustomersDataTable)dtCust.GetChanges(); da.Update(changeDT); }