Intersoft WebGrid Documentation
Walkthrough: Applying Late Binding Hierarchical
See Also Send comments on this topic.

Glossary Item Box

This walkthrough shows you how to create a late binding to Hierarchical 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 create new web application and apply late binding hierarchical

  1. Launch Visual Studio.NET 2005.
  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. Drag an ASP.Net button to WebForm.
  9. Double click the button and write the following code:

    C# Copy Code
    protected void Button1_Click(object sender, EventArgs e)
    {
       WebGrid1.ClearCachedDataSource();
       WebGrid1.RebindDataSource();
       WebGrid1.Visible = true;
    }
    

  10. On the Page_Load event handler, write the following code:

    C# Copy Code
    protected void Page_Load(object sender, EventArgs e)
    {
      body.Style.Add("overflow", "hidden");
                    
      if (WebGrid1.FlyPostBackAction == PostBackAction.RefreshData)
      {
         Button1_Click(null, null);
      }        
    }
    

  11. Double click the WebGrid instance and write the following code:

    C# Copy Code
    protected void WebGrid1_InitializeDataSource(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)
    {
       e.DataSource = GetCustomers();
    }
    

  12. On Prepare_DataBinding event handler, write the following code:

    C# Copy Code
    protected void WebGrid1_PrepareDataBinding(object sender, ISNet.WebUI.WebGrid.DataSourceEventArgs e)
    {
       if (IsPostBack)
       {
         if (!WebGrid1.IsFlyPostBack)
         {
            CreateStructure();
         }
         FormatGrid();
       }
    }
    

  13. Write the follwoing code under namespace:

    C# Copy Code
    string[] Profile1_Root = { "CustomerID", "ContactTitle", "ContactName", "Country" };
    string[] Profile2_Root = { "CustomerID", "Address", "CompanyName", "Phone" };
    
    string[] Profile1_Child = { "OrderID", "OrderDate", "Freight" };
    string[] Profile2_Child = { "OrderID", "EmployeeID", "RequiredDate" };
    
    string CurrentProfile = null;
    
    OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NorthWind.mdb");
    
    private DataSet GetCustomers()
    {
        OleDbDataAdapter da = new OleDbDataAdapter("select * from customers", oConn);
        OleDbDataAdapter da2 = new OleDbDataAdapter("select o.* from orders o inner join customers c on o.customerid=c.customerid", oConn);
    
        DataSet ds = new DataSet();
        DataTable dt1 = new DataTable("Customers");
        DataTable dt2 = new DataTable("Orders");
    
        da.Fill(dt1);
        da2.Fill(dt2);
    
        ds.Tables.Add(dt1);
        ds.Tables.Add(dt2);
    
        ds.Relations.Add(ds.Tables["Customers"].Columns["CustomerID"], ds.Tables["Orders"].Columns["CustomerID"]);
    
        return ds;
    }
    
    private void CreateStructure()
    {
        string[] UseProfileRoot = null;
        string[] UseProfileChild = null;
    
        if (CurrentProfile == "Profile1")
        {
           UseProfileRoot = Profile1_Root;
           UseProfileChild = Profile1_Child;
        }
        else
        {
           UseProfileRoot = Profile2_Root;
           UseProfileChild = Profile2_Child;
        }
    
        WebGrid1.RootTable.Columns.Clear();
        WebGrid1.RootTable.ChildTables.Clear();
        WebGrid1.LayoutSettings.Hierarchical = true;
    
        WebGrid1.RootTable.DataMember = "Customers";
        WebGrid1.RootTable.DataKeyField = "CustomerID";
        foreach (string col in UseProfileRoot)
        {
           WebGrid1.RootTable.Columns.Add(new WebGridColumn(col, col, col));
        }
    
        WebGridTable childTable = new WebGridTable();
        childTable.DataMember = "Orders";
        childTable.DataKeyField = "OrderID";
        foreach (string col in UseProfileChild)
        {
           childTable.Columns.Add(new WebGridColumn(col, col, col));
        }
    
        WebGrid1.RootTable.ChildTables.Add(childTable);
    }
    
    private void FormatGrid()
    {
        foreach (WebGridColumn col in WebGrid1.GetTableByName("Orders").Columns)
        {
           if (col.DataMember == "OrderDate" || col.DataMember == "RequiredDate")
           {
              col.DataFormatString = "d";
           }
        }
    
        WebGrid1.LayoutSettings.AllowSorting = Sorting.Yes;
    }
    

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.