Click here to Skip to main content
6,594,432 members and growing! (16,697 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Displaying drill down rows in datagrid

By Yes'B

Displaying drill down rows in datagrid
C# 1.0, Windows, .NET 1.1, ASP.NET, Visual Studio, Dev
Posted:30 Aug 2006
Views:29,880
Bookmarked:25 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 4.09 Rating: 4.29 out of 5

1

2
1 vote, 11.1%
3
4 votes, 44.4%
4
4 votes, 44.4%
5

Sample Image - Drill_down_Datagrid.jpg

 

Introduction

 

In my recent project I come across a problem of displaying drill down rows in a DataGrid. It has to show a new row immediately after the selected row to accept that row information.

 

How to use

 

Add the following template column at the end of your grid and make it Visible= �False�.

 

<asp:datagrid ...>

...

<Columns>

 

<asp:TemplateColumn Visible="False">

         <HeaderStyle BorderWidth="0px">

         </HeaderStyle>

         <ItemStyle BorderWidth="0px">

         </ItemStyle>

         <ItemTemplate>

         <ItemStyle Width="1" />

         <asp:PlaceHolder ID="ExpandedContent" Visible="false" Runat="server">

                 </td>

                 </tr>

                 <tr>

                 <td class="label" colspan="11" align="left" >

                 <asp:Label ID="IntimeLabel" Runat="server" Text="In time :">

                 </asp:Label

                 <asp:TextBox id="inTimeTextBox" cssclass="tbflat"

                      Width="130px" runat="server" ReadOnly="False"  

                      Height="20px">              

                 </asp:TextBox>

                 <asp:RequiredFieldValidator id="rfvIntime"

                      runat="server" ControlToValidate="inTimeTextBox"

                      ErrorMessage="Please enter Sample in time">*

                 </asp:RequiredFieldValidator>

                 <asp:validationsummary id="validationSummary" Runat="server"         

                          ShowSummary="False" ShowMessageBox="True">

                 </asp:validationsummary>

                 <asp:Label ID="outTimeLabel" Runat="server" Text="Out time           

                          :">

                 </asp:Label

                 <asp:TextBox id="outTimeTextBox" cssclass="tbflat"          

                          Width="130px" runat="server" ReadOnly="False"               

                          Height="20px">

                 </asp:TextBox>                                                       

                 <asp:LinkButton ID="SubmitLink" Runat="server"                       

                          cssclass="breadcrumblink"                                   

                          CommandName="Submit">Submit

                 </asp:LinkButton>

                 <asp:LinkButton ID="CancelLabel" Runat="server"                      

                          CssClass="breadcrumblink" CommandName="Cancel"              

                          CausesValidation="false">Cancel

                 </asp:LinkButton>

                 </td>

                 </td>

                 </tr>

                 </asp:PlaceHolder>                                 

                 </ItemTemplate>

</asp:TemplateColumn>

    ...

    </Columns>

...

</asp:datagrid>

 

This is dynamic content  I used in my example  it shows �Intime� ,�Outtime� labels , respective textboxes with validators and submit cancel link buttons. You can customize according to your requirements.

The next step is to include the command column in your DataGrid. You may include the column at any location. On clicking of this command column the grid expands/collapses.

 

 

<asp:datagrid ...>

...

    <Columns>

     <asp:TemplateColumn>

         <HeaderTemplate>

         </HeaderTemplate>

                 <ItemTemplate>

                 <asp:LinkButton ID ="btnExpand" CommandName="Expand" Runat           

                          ="server" >DrillDown

                 </asp:LinkButton>

                 </ItemTemplate>

         </asp:TemplateColumn>

    ...

    </Columns>

...

</asp:datagrid>

 

Use following code to link OnItemCommand event to call DrilldownGrid_OnItemCommand"

 

<asp:datagrid OnItemCommand="DrilldownGrid_OnItemCommand"...>

    ...

         <Columns>

    ...

         </Columns>

...

</asp:datagrid>

 

Write the following code in DrilldownGrid_OnItemCommand method

 

public  void DrilldownGrid_OnItemCommand(object source                                

                 ,System.Web.UI.WebControls.DataGridCommandEventArgs e)

                 {

                 //Get expand Content

                 PlaceHolder  expandedContent   = (PlaceHolder)e.Item.                

                 FindControl("ExpandedContent");

                 switch(e.CommandName)

                          {

                 //If command name is "Expand" then modify the visibility of

                 expand content

                 case "Expand":

 

                          if(gridExpandState==false)

                          {

                          gridExpandState=!gridExpandState;

                          expandedContent.Visible = ! expandedContent.Visible;

                          DrilDownGrid.Columns [DrilDownGrid.Columns.Count-           

                          1].Visible =!DrilDownGrid.Columns                  

                          [DrilDownGrid.Columns.Count-1].Visible ;

                          }

                          break;

                          //Show hide expand content

                          case "Cancel":

                          {

                          gridExpandState=!gridExpandState;

                          expandedContent.Visible = ! expandedContent.Visible;

                          DrilDownGrid.Columns [DrilDownGrid.Columns.Count-           

                          1].Visible =!DrilDownGrid.Columns                  

                          DrilDownGrid.Columns.Count-1].Visible ;

                          }

                          break;

                 }

         }

 

gridExpandState is the Boolean variable used to maintain the current state of DataGrid. Define this as page property as shown.

 

protected bool gridExpandState

{

         get

         {

         object gridExpandStateObject = ViewState["gridExpandState"];

         return (gridExpandStateObject == null) ? false :    (bool)gridExpandStateObject;

         }

         set

         {

         ViewState["gridExpandState"] = value;

         }

}

 

Your DataGrid is now ready.

 

An example project is available for download. Down load and change connection string to workwith.

 

Acknowledgements

 

I must thank the all my colleagues for their help and support.

 

Usage and Copyrights

 

You may freely use/modify the supplied file for your own usage, as long as you retain the acknowledgements presented above. Please don't forget to rate this article. Thanks and good luck.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yes'B


Member

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralGreat article! Pinmembertemplariosp7:27 8 Jul '08  
GeneralThank you PinmemberMember 45819418:48 14 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Aug 2006
Editor:
Copyright 2006 by Yes'B
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project