Intersoft WebGrid Documentation
Walkthrough: Creating Updatable WebGrid using DataTable
See Also Send comments on this topic.

Glossary Item Box

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:

 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

  1. Launch Visual Studio.NET 2008.
  2. Click on File menu, then select New and click Web Site.
  3. Select ASP.NET Web Site in the Template box and set Location to HTTP.
  4. Named the Web Site and click OK.
  5. Right-click on Project's name and select Add New Item.
  6. Select Intersoft AppForm in the My Templates box and named it as Walkthrough.aspx.
  7. Drag WebGrid instance from ToolBar to WebForm.
  8. In the Solution Explorer, right-click on App_Data and select Add Existing Item.
  9. Browse and add NorthWind.mdb in C:\Program Files\Intersoft Solutions\Data (Default installation folder).
  10. Create DataSet (dsNorthWind) in App_code and go to Server explorer.



  11. Choose Northwind and drag and drops all tables into dataset.



  12. Create a new class object (dsNorthwind_Extended.cs) in App_code.



  13. 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; }       
          }
       }
    }
    

  14. Press F4 on WebGrid via VS Design mode and set the following event for server-side.



  15. 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());           
          }       
       }   
    }
    

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.