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

Glossary Item Box

This walkthrough shows you how to Bind WebGrid to a DataSource at RunTime.

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 DataSource in WebGrid at RunTime.

  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. Add following DataSet inside the code-behind under the namespace then paste in the codes below:

    C# Copy ImageCopy Code
    public DataSet LoadData()
    { 
      OleDbCommand cmd = oleDbConnection1.CreateCommand();
      cmd.CommandType = CommandType.Text;
      cmd.CommandText = "SELECT * FROM CUSTOMERS";
      
      OleDbDataAdapter da = newOleDbDataAdapter();
      da.SelectCommand = cmd;
       
      DataSet ds = new DataSet();
      da.Fill(ds);
      
      ds.Tables[0].TableName = "Customers"; 
      
      return ds;
    }                             
    
  8. Double click the WebGrid and add following codes inside the InitializeDataSource event handler:

    C# Copy ImageCopy Code
    e.DataSource = LoadData();                           
    

  9. Then, it will automatically retrieve its structure by using RetrieveStructure() method in PrepareDataBinding event handler:

    C# Copy ImageCopy Code
    if(!IsPostBack)
    {
      WebGrid1.RetrieveStructure();
    }                              
    
  10. Compile and run the WebForm.

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.