Home > SOA Tips > .NET Developer > Dynamic drill-down DataGrid
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Dynamic drill-down DataGrid


Bob Tabor
10.11.2002
Rating: -5.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This tip demonstrates how to "hack" the DataGrid. By hack in this context I mean to use it in (what I'm guessing was) an unintended way.

Suppose you have two related DataTables, and you want to demonstrate the hierarchical nature of the data. You want it to work the way the DataGrid in WinForms and Microsoft Access works, where a tree-like plus and minus symbol allow you to see all the related records for a given parent record. I'll show how to accomplish this in ASP.NET.

See Figure 1.

I'll show how to use an XML file in this capacity, populating it into a DataSet for easy data binding. In the next tip, I'll show how to dynamically pull records from a data source based on the user's selection, so you don't have to fetch all the related records to all the parent rows at the same time (which could be a performance nightmare).

I need to acknowledge my friend David Findley for turning me on to this technique. Admittedly, he is the real genius behind this tip. Also, I've created a complete 16-minute screen-cam video tutorial, complete with commentary for this tip available in the Supporter's area at www.LearnVisualStudio.NET.

I encourage you to download the source code for this tip (written in Visual Basic .NET) to examine it. I'll refer to it in a narrative format, so you'll need it close by.

The MusicCollection.xml has a list of David's favorite albums in the following style:

<MusicCollection>
 <album title="Dark Side Of The Moon" artist="Pink Floyd" year="1973">
  <track title="Speak To Me" length="4:00"/>
  <track title="On The Run" length="3:33"/>
  <track title="Time" length="7:06"/>
.
.
      </album>

To get the XML file into a DataSet, the Global.asax file contains the following code:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
        Dim ds As New DataSet()
        ds.ReadXml(Server.MapPath("MusicCollection.xml"))
        Session("MusicCollection") = ds
    End Sub

You'll notice that there is no schema in this project. The schema is inferred in this example.

Now that the DataSet is populated and loaded in the Session object, we can turn our minds to the meat of the discussion. WebForm1.aspx has a DataGrid with 5 columns. The middle columns will bind to the album information like artist, title and year. The first column (a template column) will contain our + and –graphics for the given album row. The last column (also a template column) is defined in a rather odd way:

<asp:TemplateColumn>
 <ItemStyle Width="1" />
 <ItemTemplate>
 <asp:PlaceHolder ID="ExpandedContent" Visible="False" Runat="server">
   </td></tr>
  <tr>
   <td width="9"> </td>
   <td colspan="3">
.
.
.

The column contains a PlaceHolder object called ExpandedContent that basically is hidden (Visible="False") until someone clicks the little + icon, at which time the event defined in the code behind for this web form is fired. (I'll talk about that in a moment.)

What is displayed is some HTML table cell and row tags that effectively intercept the current output that will be generated by the DataGrid with our own special implementation, namely we are telling the table (when its drawn) to close the current cell, the current row, and now add a new row with one blank column (for spacing) and another column (spanning three columns) that we'll use to display a second DataGrid.

<asp:DataGrid id="Datagrid2" DataSource='<%# 
Container.DataItem.CreateChildView("album_track") %>' runat="server" 
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" 
CellPadding="3" GridLines="Horizontal" Width="503px" 
AutoGenerateColumns="False">
.
.
.
 <Columns>
  <asp:BoundColumn DataField="title" HeaderText="Title" />
  <asp:BoundColumn DataField="length" HeaderText="Length" />
 </Columns>
</asp:DataGrid>

This DataGrid will contain the columns bound to the title and length nodes of the XML file for each track. Notice that the DataGrid's DataSource is set to the Container.DataItem.CreateChildView("album_track"). I discussed the CreateChildView method in previous tips, but let me remind you that the name of the relation that is defined here ("album_track") must match the two names of the related DataTables in your DataSet or else this will NOT work!

You might be wondering how much effort is involved in the code behind to accomplish the hiding and displaying on the secondary grid, but surprisingly there is very little code.

    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
 System.Web.UI.WebControls.DataGridCommandEventArgs) Handles 
DataGrid1.ItemCommand
        Select Case e.CommandName
            Case "Expand"
                Dim ExpandedContent As PlaceHolder = e.Item.Cells
(DataGrid1.Columns.Count - 1).FindControl("ExpandedContent")
                ExpandedContent.Visible = Not ExpandedContent.Visible


                Dim btnExpand As ImageButton = e.Item.Cells(0).FindControl("btnExpand")
                If btnExpand.ImageUrl = "˜/Images/Plus.gif" Then
                    btnExpand.ImageUrl = "˜/Images/Minus.gif"
                Else
                    btnExpand.ImageUrl = "˜/Images/Plus.gif"
                End If
        End Select
    End Sub

The ItemCommand event for the first grid is employed. We evaluate the e.CommandName to determine whether we are handling the correct command from the grid. But wait. Where is the CommandName set? Actually, we set as we defined the ImageButton that contains the + and –images in the first column:

<asp:ImageButton ImageUrl="˜/Images/Plus.gif" CommandName="Expand" 
ID="btnExpand" Runat="server">

The DataGrid passes this CommandName through to the code behind event handler and allows us to do whatever we wish with the information that our ImageButton was clicked. In this case, we do a quick check to see whether we are currently in "expanded" mode or "closed" mode, and perform the opposite action. In other words, if we are currently "expanded", then make is "closed". Expanding and closing involves setting the visibility of the appropriate PlaceHolder object ("ExpandedContent") to True or False.

As I alluded to earlier in this tip, next time I'll demonstrate how to take this a step further by dynamically adding data into the child DataTable as the user requests it. I'll also make sure to not have to grab information again, in case the user wants to expand the child grid again.


About the Author

Robert Tabor is a Microsoft Certified Professional in Visual Basic with over six years of experience developing n-tier Microsoft-centric applications for some of the world's most prestigious companies and consulting organizations, such as Ernst & Young, KPMG, Cambridge Technology Partners, Sprint, American Heart Association, and the Mary Kay Corporation. Bob is the author of Microsoft .NET XML Web services by Sams Publishing, and contributes to SoapWebservices.com and LearnVisualStudio.NET. He is currently working on initiatives within Mary Kay, the second largest eCommerce site in retail volume on the net, of how to utilize .NET within their e-business group.


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Platforms and Servers
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
Decision time: .NET or J2EE?
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices
Working with PDFs in a .NET environment
Displaying errors with the error provider

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices

Microsoft .NET Web services
Microsoft preps .NET 4.0 - framework improves on REST, MVC, JQuery support
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Microsoft .NET Web services Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts