Home > Microsoft .Net Development Tips > ASP.NET Development > Dynamic drill-down DataGrid
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ASP.NET DEVELOPMENT

Dynamic drill-down DataGrid


Bob Tabor
10.11.2002
Rating: -4.64- (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 SearchWinDevelopment.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
ASP.NET Development
Dundas Map for .NET kicks up geographic visualization
Return to CodePlex: Into the Sandcastle…
VBScript Tutorial
Use PHP with Visual Studio to create Web sites
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Visual Studio 2008 and .NET Framework 3.5 SP1 introduces ADO .NET Entity Designer
XAML as an everywhere presentation format
Introduction to Silverlight 1.0: Building your first Silverlight application
Creating custom ASP.NET 2.0 profile providers
Book excerpt: Silverlight 1.0 explained

ASP.NET development best practices
LINQ Learning Guide: LINQ and Web applications
VB code download home page
VB code: File manipulation downloads
Localization practices for .NET 2.0: It's still about the architecture
Creating custom ASP.NET 2.0 profile providers
.NET development in the trenches
Microsoft developers balancing age-old issues, barrage of new technology
SearchVB.com's Podcast Page
Visual Studio 2008 Learning Guide: Web development
Top .NET tips of 2007 (so far)

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
136 browser colors with names  (SearchWinDevelopment.com)
ASP.NET  (SearchWinDevelopment.com)
browser  (SearchWinDevelopment.com)
Document Object Model  (SearchWinDevelopment.com)
domain name  (SearchWinDevelopment.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.

About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




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