advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the sample application.
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 6 users have rated this item.
Nested DataGrids Simplify Hierarchical Data Display (cont'd)
Finding Your Orders
The Orders Advanced Search page allows users to locate customers' previous orders and does double duty as an order history report. The search criteria page takes zero or more customer demographics, an optional customer, and a required date range. More relevant is what the page returns:
advertisement


 
Figure 6. Orders Advanced Search Schema: This schema shows the tables and relationships returned by the Orders Advanced Search page.
  • Orders—including order date, customer and shipper.
  • Products related to each Order—including product name and quantity.
  • A count of products ordered for the current customer and current product, for each month in the date range.
The strongly typed dataset that holds the search results is shown in Figure 6. You can find the schema in the download that accompanies this article.

The normalized approach to holding data greatly simplifies displaying it hierarchically in the user interface. Figure 7 shows the loops in the page.

ProductMonthCounts—Where Things Get Interesting
There is a lot going on in the page with five separate multi-record controls nested three levels deep. First, each is bound using a different approach. The outermost loop for products is bound in the code-behind page; the nested loops are bound using data-binding syntax; and the innermost loop, for ProductMonthCounts, is bound in the ItemDataBound event of the nested OrderDetails (products) Repeater.

 
Figure 7. Loops In Search Results: The arrows show the inner and outer loops required to display the hierarchical data.
The innermost loop is the most complex. You have to bind it in the ItemDataBound event primarily because it requires a custom sort order. The custom sorting is required because the database doesn't return months where no products were sold; the middle tier inserts those records. Consequently, the order of records is incorrect in the dataset. The relevant section of code sorts by the FirstDayInMonth field and binds to records that match the current product and customer:

   string strCriteria = String.Format(
     "ProductId={0} AND CustomerID='{1}'", 
     intProductId,
     strCustomerID);
   rptProductCountMonths.DataSource = 
     tdsSearchResults.ProductMonthCounts.Select(
     strCriteria, 
     "FirstDayInMonth");
   rptProductCountMonths.DataBind();
Another interesting aspect of the innermost loop is the code that dynamically sets the background color to a deeper shade depending on the number of orders sold.

   <td style='background-color: <%# GetBgColorFromQuantity(
     (int)((DataRow)Container.DataItem)["Count"], 
     false) %>'> … </td>
GetBgColorFromQuanity() takes a quantity of products sold, computes red, green, and blue values and returns an HTML color with a call to:

   System.Drawing.ColorTranslator.ToHtml(
     System.Drawing.Color.FromArgb(intRed, intGreen, intBlue)
     ); 
This is yet another application of the data-binding syntax.

Even if you don't need to display hierarchical data frequently, a thorough understanding of control hierarchy, data-binding syntax, and events of multi-record controls will empower your code and help you create more robust and maintainable solutions. At first, using nested controls may seem more complicated than the traditional approach of building HTML tables in code, but when you're asked to change your UI the day the application is due, you'll be glad you did.

Previous Page: Other Data Binding Syntax Applications  


Lee Richardson is founder and Chief Executive Officer of Automated Architecture, Inc., and is the author of the Blue Ink application and the JAG software development methodology. Lee works as a senior consultant for the Headstrong consulting company, specializing in rapid application development technologies. He has nine years of software development experience, is a Microsoft Certified Solution Developer (MCSD), and a Project Management Professional (PMP).
Page 1: IntroductionPage 3: Other Data Binding Syntax Applications
Page 2: To Normalize or Not to NormalizePage 4: Finding Your Orders
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs