Intersoft WebGrid Documentation
Walkthrough: Binding WebGrid to IList DataSource
See Also Send comments on this topic.

Glossary Item Box

This walkthrough shows you how to bind IList DataSource to WebGrid.

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 2005 Application.

 Step-By-Step Instructions

To Bind IList DataSource in WebGrid.

  1. Launch Visual Studio.NET 2005.
  2. Click on File menu, then select New and click Project.
  3. Select Visual C# Project in Project Types.
  4. Select ASP.NET Web Application in the Template box.
  5. Specify the Project's Location and click OK.
  6. Drag WebGrid into WebForm.
  7. Double click on the WebForm to write Page_Load codes:

    C# Copy ImageCopy Code
    ArrayList aList = new ArrayList();
    aList.Add( new Invoices(1, "INV-001", DateTime.Parse("2/1/2003"), "John Smith", 5, 750, 750 * 5) );
    aList.Add( new Invoices(2, "INV-002", DateTime.Parse("2/2/2003"), "Steven Buchanan", 10, 450.7, 4507) );
    aList.Add( new Invoices(3, "INV-003", DateTime.Parse("2/10/2003"), "Alexander King", 25, 700, 25 * 700) );
    WebGrid1.DataSource = aList;
    WebGrid1.RetrieveStructure();
    WebGrid1.DataBind();                       
    
  8. Add following class inside the code-behind under the namespace or create a new class (.cs file), then paste in the codes below:

    C# Copy ImageCopy Code
    public class Invoices                             
    {                               
      private int mInvoiceId;
      private string mInvoiceNo;
      private DateTime mInvoiceDate;
      private string mCustomerName;
      private int mQty;
      private double mUnitPrice;
      private double mTotal;
      public Invoices (int InvoiceId, string InvoiceNo, DateTime InvoiceDate,
                        string CustomerName, int Qty, double UnitPrice, double Total)
      {
         this.mInvoiceId = InvoiceId;
         this.mInvoiceNo = InvoiceNo;
         this.mInvoiceDate = InvoiceDate;
         this.mCustomerName = CustomerName;
         this.mQty = Qty;
         this.mUnitPrice = UnitPrice;
         this.mTotal = Total;
      }
      public int InvoiceId
      {
         get { return this.mInvoiceId; }
      }
      public string InvoiceNo
      {
         get { return this.mInvoiceNo; }
      }
      public DateTime InvoiceDate
      {
         get { return this.mInvoiceDate; }
      }
      public string CustomerName
      {
         get { return this.mCustomerName; }
      }
      public int Qty
      {
         get { return this.mQty; }
      }
      public double UnitPrice
      {
         get { return this.mUnitPrice; }
      }
      public double Total
      {
         get { return this.mTotal; }
      }
    }                              
    
  9. Now, open WebGrid Designer by right clicking WebGrid and choosing WebGrid.NET Designer. Select the Allow Sorting properties in QuickStart - Popular Settings - Main Features section.
  10. Compile and run the WebForm.

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.