This walkthrough shows you how to create updatable WebGrid using DataTable in BatchUpdate mode (Insert Identity / AutoIncrement ID Scenario).
During this walkthrough, you will learn how to do the following:
- Create Dataset
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 ObjectDataSource
- 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).
- Create DataSet (dsNorthWind) in App_code and go to Server explorer.
- Choose Northwind and drag and drops all tables into dataset.
- Create a new class object (dsNorthwind_Extended.cs) in App_code.
- Refer the namespace to dsNorthwindTableAdapters and add methods to select identity. Here are the following details.
C# Copy Code usingSystem.Data.OleDb;namespacedsNorthwindTableAdapters { public partial class OrdersTableAdapter : System.ComponentModel.Component { public OleDbDataAdapter DataAdapter { get { return this.Adapter; } } } }
- Press F4 on WebGrid via VS Design mode and set the following event for server-side.
- Bind WebGrid traditionally and use the following methods for identity scenario.
C# Copy Code dsNorthwindTableAdapters.OrdersTableAdapter daOrders; protected void WebGrid1_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { dsNorthwind.OrdersDataTable dt = new dsNorthwind.OrdersDataTable(); dsNorthwindTableAdapters.OrdersTableAdapter daOrders = new dsNorthwindTableAdapters.OrdersTableAdapter(); daOrders.Fill(dt); e.DataSource = dt; } protected void WebGrid1_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e) { WebGrid1.RetrieveStructure(); } protected void WebGrid1_InitializeLayout(object sender, LayoutEventArgs e) { e.Layout.AllowAddNew = AddNew.Yes; e.Layout.AllowBatchUpdate = true; e.Layout.AllowDelete = Delete.Yes; e.Layout.AllowEdit = Edit.Yes; } protected void WebGrid1_BatchUpdate(object sender, BatchUpdateEventArgs e) { dsNorthwind.OrdersDataTable dt = (dsNorthwind.OrdersDataTable)WebGrid1.GetCachedDataSource(); dsNorthwind.OrdersDataTable changeDT = (dsNorthwind.OrdersDataTable)dt.GetChanges(); daOrders = new dsNorthwindTableAdapters.OrdersTableAdapter(); daOrders.DataAdapter.RowUpdated += new OleDbRowUpdatedEventHandler(DataAdapter_RowUpdated); daOrders.Update(changeDT); } void DataAdapter_RowUpdated(object sender, OleDbRowUpdatedEventArgs e) { // Conditionally execute this code block on inserts only. if (e.StatementType == StatementType.Insert) { if (e.Row.Table.TableName == "Orders") { /* Retrieve the new identity and call UpdateRowIdentity to process the new identity. */ OleDbCommand cmdNewID = new OleDbCommand("SELECT @@IDENTITY", e.Command.Connection); WebGrid1.GetTableByName("Orders").UpdateRowIdentity("OrderID", e.Row["OrderID"], cmdNewID.ExecuteScalar()); } } }
Tasks
Walkthrough: Creating Updatable Hierarchical WebGrid using DataSet
Walkthrough: Creating Updatable WebGrid using ISDataSource
Walkthrough: Creating Updatable Hierarchical WebGrid using ISDataSource
Walkthrough: Creating Updatable WebGrid using Custom Object
Other Resources
Walkthrough Topics
How-to Topics