Click here to Skip to main content
Click here to Skip to main content

ASP.NET GridView Columns with Collapsible Panel Embedded

By , 29 Sep 2009
 
DataGridView_DIVTag - Click to enlarge image

DataGridView_DIVTag - Click to enlarge image

Introduction

My job has required me to read quite a bit of code written by other people, and over the years I've found that there are some problems pertaining to gridview in showing up huge data. It's possible to have many columns in a gridview to display the unwanted and wanted columns with horizontal scrollbars. There may be situations where we will need to display only a few columns and if required, we will have to give detailed information. In this article, I will explain how to have a collapsible panel in a column of ASP.NET GridView. This article will help the readers to learn about integration of collapsible panel in GridView columns.

Background

In my example above, I have attached two images which will explain all about this article. I have two columns displayed in ASP.NET gridview. Both the column values are from SQL SERVER 2005 database. Screen1 is how the screen will look like on page load. The first column in the grid is a readonly column. The second column in the grid is link column to which the Collapsible panel is integrated. When the user hovers over the text in the second column, the mouse icon is changed to handtool and onClick the panel expands to show information either from the database or hardcoded text. The expansion and collision of the text is pure client side.

Using the Code

Here in this example, I am trying to create two datacolumns and add it to a datatable. I will fill in the datacolumns from the filled in dataset. Any user who uses this code can create the table and use the insert scripts provided in the zip file to test this application or else have your technology for getting the data. After getting the data, now its our turn to fill in the datacolumns. To fill in the datacolumns, I will iterate through the datarows of the datatable. As I mentioned earlier, we have two columns in the datagrid. I am filling the first column with the text which is returned from the database. I am attaching a collapsible panel to the second column.

function toggleLayer(whichLayer)
{
if (document.getElementById)
{
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}

Now let me tell you how to add the collapsible panel. It's just a simple div tag added along with a JavaScript function named toggleLayer in an anchor tag. As the C# code contains HTML tags, I am unable to show the code here. toggleLayer is JavaScript function which takes the rowId as the parameter and displays or hides the respective panel. As the Div tag is created dynamically inside a foreach loop, each div tag will have its own uniqueId across the page.

foreach (DataRow dr in ds.Tables[0].Rows)
{
  myDr = dt.NewRow();
  myDr[0] = dr[0].ToString();

  myDr[1] = "<a href=\"javascript:toggleLayer('row" + count 
  	+ "');\" class=DivPanelTextBold>" + dr["SubModule"].ToString() + "</a>"
 	+ "&nbsp;&nbsp;&nbsp;&nbsp;<div id='row" + count + "' class='DivPanel'>"
 	+ "<TABLE cellSpacing='2' cellPadding='2' width='100%' border='0'>"
 	+ "<tr><td class='La_Ma_Text'>&nbsp;</td><
 	td align=right><a href=\"javascript:toggleLayer('row" + count 
 	+ "');\" class=La_Ma_Link><img src='Close.gif' 
 	border=0 align='absmiddle'></a></td></tr>";

 myDr[1] = myDr[1] + "<tr><td class='DivPanelText' width='158px'>
 	&nbsp;Cell Value: </td><td class='DivPanelText' width='152px'>
 	" + dr["SubModule"].ToString() + "</td></tr>";

 myDr[1] = myDr[1] + "<tr><td><b> Something </b></td></tr>";
 myDr[1] = myDr[1] + "</TABLE>" + "</div><br>";
 
 dt.Rows.Add(myDr);
 count++;
}

In the above foreach loop, we are iterating through each table row and creating a new datarow and append data to it. As usual to the first column, I am attaching whatever is retrieved from the database. To the Second column, myDr[1], I am assigning an anchor tag as well as div tag with unique Id for each div tag created. When the grid is bound to the page, when the user clicks on any of the links available in the second column, it calls toggleLayer (explained above) JavaScript function and passes the rowId. So if the div tag was in collapsed state, it will expand to show the details or else it will collapse. Likewise, we can have any kind of HTML embedded to any number of columns.

Points of Interest

This is a very simple technique which can help developers to have the best UI.
toggleLayer: JavaScript function to display or hide respective div tag.

History 

This is my initial version. Please email me for any doubts related to this article or post your question in the discussion forum below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Padoor Shiras
Web Developer Proteans Software Solutions Pvt Ltd.
India India
Member
Shiras AbdulRahman Currently working with Proteans Software Solutions Bangalore.
 
Proteans a CAMO group company is an outsourcing company focusing on software product development and business application development on Microsoft Technology Platform. "Committed to consistently deliver high-quality software products and services through continual improvement of our knowledge and practices focused on increased customer satisfaction.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalinteresting - have 5memberPranay Rana4 Jan '11 - 0:59 
Excellent
For any question : http://pranayamr.blogspot.com/
 
vote my article :

Learn SQL to LINQ ( Visual Representation )


Calling WCF Services using jQuery

GeneralMy vote of 5memberBit-Smacker16 Oct '09 - 9:26 
Hi!
 
I needed something flexible and dynamic that allowed me to add different controls and fields to each row, depending on column values or threshold triggers. Now, I can just inject anything into the for loop that builds the datarow!
 
The data that I'm accessing comes from a mix of SQL and ODBC datasources, which makes binding impossible. I'll need to programmatically break it out into sub-queries and add it within the loop.
 
Thank you for keeping it flexible, without binding!
GeneralRe: My vote of 5memberPadoor Shiras25 Oct '09 - 19:23 
Hi
Its possible to add dynamic controls instead of the html posted in my article. depending on your value from the datbase you can always play around and create. Please let me know whether you need any further information.
GeneralMy vote of 1membermambo_229 Sep '09 - 9:51 
crazy code, not a good solution
GeneralRe: My vote of 1memberVasanth.S.R30 Sep '09 - 21:56 
I do not see a single article by you nor a reasonably good reply or question in any of the forums. Please post a good solution if you feel this is a "crazy code"
General[My vote of 1] Dangerous way to embed html in your code..membererik25629 Sep '09 - 3:37 
Dangerous way to embed html in your code.. Why not let the Standard web controls handle it based on your browsers capabilities? its what they are designed for?
 
its all nice coding something cool and visual like this, but of what use would it be?
GeneralRe: [My vote of 1] Dangerous way to embed html in your code..memberPadoor Shiras29 Sep '09 - 4:13 
Basically the enduser loves to have catching UI. This was we can limit the UI visibility by removing the horizontal scrollbar, by hiding all the information in a hidden form.
 
Any webcontrols are presented as HTML after loading. And in the code we are not allowing any inputs from the enduser. So i believe there is no harm in embedding html.
GeneralRe: [My vote of 1] Dangerous way to embed html in your code..membererik25629 Sep '09 - 4:44 
I tested your concept on my machine with a large grid, I found the vertical scroll bar appeared and dissapeared as the panel was shown and then hidden. When scrolled partially down the grid this made the display job. hardly a user friendly display.
 
Now if you extended the concept to have fixed headers on the gridview, scrolling within the window, saving of current scroll position so that the gridview scrolling does not jump then that would be better.
 
Personally tho, I feel a tooltip solution is more ideal for displaying more information on a gridview cell.
GeneralRe: [My vote of 1] Dangerous way to embed html in your code..memberPadoor Shiras29 Sep '09 - 5:10 
Thanks eric for your suggestion. I would incorporate static headers as the second version of this article.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 29 Sep 2009
Article Copyright 2009 by Padoor Shiras
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid