This walkthrough shows you how to create a late binding to Hierarchical 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.
- Use WebGrid's InitializeDataSource event to bind.
- Use late binding scenario.
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
- Launch Visual Studio.NET 2005.
- Click on File menu, then select New and click Web Site.
- Select ASP.NET Web Site in the Template box and set Location to HTTP.
- Named the Web Site and click OK.
- Right-click on Project's name and select Add New Item.
- Select Intersoft AppForm in the My Templates box and named it as Walkthrough.aspx.
- Drag WebGrid instance from ToolBar to WebForm.
- Drag an ASP.Net button to WebForm.
- 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; }
- 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); } }
- 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(); }
- 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(); } }
- 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; }