This walkthrough shows you how to bind IList DataSource to WebGrid.
During this walkthrough, you will learn how to do the following:
- Connect to a Microsoft Access database using Microsoft Jet 4.0 OLE DB Provider.
- Create new Class.
- Use Page_Load event to add ArrayList, set DataSource, Retrieve Structure and DataBind.
- Bind IList DataSource to WebGrid.
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.
- Launch Visual Studio.NET 2005.
- Click on File menu, then select New and click Project.
- Select Visual C# Project in Project Types.
- Select ASP.NET Web Application in the Template box.
- Specify the Project's Location and click OK.
- Drag WebGrid into WebForm.
- Double click on the WebForm to write Page_Load codes:
C# Copy 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();
-
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 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; } } }
- 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.
- Compile and run the WebForm.