WebGrid provides feature to do binding after PostBack, which can be called Late Binding.
In this topic, you will learn the basic thing on how to implement Late Binding.
To Bind WebGrid after PostBack
- Drag Server Button to your WebForm.
- On ButtonClick event handler, add the following code:
C# Copy Code private void Button1_Click(object sender, System.EventArgs e) { CurrentCountry = DropDownList1.SelectedValue; CurrentProfile = RadioButtonList1.SelectedValue; WebGrid1.ClearCachedDataSource(); WebGrid1.RebindDataSource(); WebGrid1.Visible = true; }
- Double click on the WebForm to write Page_Load codes.
C# Copy Code private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!IsPostBack) { OleDbDataAdapter da = new OleDbDataAdapter("select * from employees", oleDbConnection1); DataTable dt = new DataTable(); da.Fill(dt); DropDownList2.DataSource = dt; DropDownList2.DataTextField = "FirstName"; DropDownList2.DataValueField = "EmployeeID"; DropDownList2.DataBind(); } else { if (WebGrid1.FlyPostBackAction == PostBackAction.RefreshData) { // simulate button click event when refresh icon is clicked Button1_Click(null, null); } } }
- Add the following code under the 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; string CurrentCountry = null; private DataSet GetCustomers(string Country, string Profile) { if (Country == null) return null; OleDbDataAdapter da = new OleDbDataAdapter("select * from customers where country='" + Country + "'", oleDbConnection1); OleDbDataAdapter da2 = new OleDbDataAdapter("select o.* from orders o inner join customers c on o.customerid=c.customerid where c.country='" + Country + "'", oleDbConnection1); 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"]); CurrentProfile = Profile; 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.RootTable.Columns) { if (col.DataMember == "CustomerID") col.CellStyle.HorizontalAlign = HorizontalAlign.Right; } foreach (WebGridColumn col in WebGrid1.GetTableByName("Orders").Columns) { if (col.DataMember == "OrderDate" || col.DataMember == "RequiredDate") { col.DataFormatString = "d"; } else { col.CellStyle.HorizontalAlign = HorizontalAlign.Right; } } WebGrid1.LayoutSettings.AllowSorting = Sorting.Yes; }
- Double click the WebGrid and add the following code under InitializeDataSource event handler.
C# Copy Code e.DataSource = GetCustomers(CurrentCountry, CurrentProfile);
- Then, it will automatically retrieve its structure by using RetrieveStructure() method in PrepareDataBinding event handler.
C# Copy Code if (IsPostBack) { if (!WebGrid1.IsFlyPostBack) { CreateStructure(); } FormatGrid(); }
- Add the following codes under InitializeTable event handler.
C# Copy Code private void WebGrid1_InitializeTable(object sender, ISNet.WebUI.WebGrid.TableEventArgs e) { if (IsPostBack && !WebGrid1.IsFlyPostBack && e.Table.IsRootTable) { if (CheckBox1.Checked) { // get customers rows where order's employeeid = selected value OleDbDataAdapter da = new OleDbDataAdapter("select distinct customerid from orders where employeeid=" + DropDownList2.SelectedValue, oleDbConnection1); DataTable dt = new DataTable(); da.Fill(dt); foreach(WebGridRow row in WebGrid1.RootTable.Rows) { foreach(DataRow dr in dt.Rows) { if (row.Cells.GetNamedItem("CustomerID").Text == dr["CustomerID"].ToString()) row.ExpandChildRow(); } } } } }
- Compile and run the WebForm.
Tasks
Walkthrough: Binding WebGrid to a DataSource at RunTime
Walkthrough: Binding WebGrid to XML-based DataSource
Walkthrough: Binding WebGrid to IList DataSource
Walkthrough: Binding WebGrid with SQL Server 2000 Tables
Walkthrough: Binding WebGrid with a XSD schema and a XML file data
Other Resources
Walkthrough Topics
How-to Topics