ProgTalk - Your archive for all source code

DataGrid - Master/Child/Slave Details in ASP.NET, C# - Expand/Collapse Rows (Master/Detail View)

ProgTalk » Articles » .NET, C#, ASP.NET, Javascript » DataGrid - Master/Child/Slave Details in ASP.NET, C# - Expand/Collapse Rows (Master/Detail View)
 
Author: Rajib A.
Page Views: 80421
Average Article Rating: rating starrating starrating starrating starrating star
 

DataGrid for Master/Child (Master/Slave) Details Using
ASP.NET, C#, and JavaScript (Expand / Collapse Rows) Version 1
-------------------------------------------------------------------------
Version 2 exists for this article:

http://www.progtalk.com/ViewArticle.aspx?ArticleID=16

Looking for this code in VB.NET?

VB.NET Version

This grid will allow users to view master/child or master/slave details of records from a SQL database.  It will allow them to view the details through the client side script, and will maintain the state of expanded rows during any postback.  Here is sample screen shot of how the grid will look:

Before Expanding Rows:  The grid will load with a + next to all grouped items.


After Expanding Rows:  The selected rows will expand, and the + will change to a - on client side using javascript.


What Happens if you change the page?  The new page is loaded with everthing collapsed.


Expanding Rows on new page:  The new page will expand the selected rows, and the + will change to a - client side using javascript.


What happens if a non grid event is fired?  This will not effect how the grid is being viewed by the user, as it maintains the expansion/collapse state.


--------------------------------------------------------------------------------------------------------------------

Background

This is a custom grid which can be made into a control. For now it was done with Northwind and an asp.net page. In the future I will most likely make this into a custom control.

--------------------------------------------------------------------------------------------------------------------

Using the code

The following code is very basic, and will allow a user to view a grid in a master/child manner. Most of the code has notes, and is pretty easy to understand. The trick is to add a new row below the row the grid generates. Inside that row we create
a div section of the details. Then we bind it with the javascript to show and hide the div sections. This will allow client side viewing of the details giving a more rich feel.

To use the code, all you have to do is set your connection string in the default.aspx page and run it. If you wish to use it with your own database, change the query in the following methods:

1. BindData();    //For the Master view section

2. DataGrid1_ItemDataBound();    //For the Child view section.

Good Luck!!!

Note: The javascript code functions are on a js file which can be found once you download the full source code. If you guys find any bugs, please feel free to let me know, so I can modify the code for others.

* I do not hold any responsibility for the use of this code. Please use at your own risk *

--------------------------------------------------------------------------------------------------------------------

private string connectionstring = "server=SERVERNAME;database=Northwind;uid=USERNAME;password=PASSWORD;";

private void Page_Load(object sender, System.EventArgs e)
{
//Clear the contents of the Label this.LabelPostBack.Text = "";
if ( !Page.IsPostBack )
{
//Bind Master Details BindData(); } else
{
//If it is a postback that is not from the grid, we have to expand the rows //the user had expanded before. We have to check first who called this postback //by checking the Event Target if ( Request["__EVENTTARGET"] != null)
{
string strEventTarget = Request["__EVENTTARGET"].ToString().ToLower();
if ( strEventTarget.IndexOf("datagrid1") == -1)
{
//Expanded what ever the user had expanded by Setting Mode //which will be used in ItemDataBound method. ViewState["Mode"] = "ShowDetails";

//Not caused by datagrid //We need to rebind data and generate scripts.     BindData(); } else
{
ViewState["Mode"] = null;
}
}
}
}

//Display Postback contents in hidden field private void ButtonSample_Click(object sender, System.EventArgs e) { LabelPostBack.Text = "A Postback has occurred. txtExpandedFields has contents: <BR/><B>" +
txtExpandedFields.Text + "</B>";
}

//Bind Data for Master Grid Information private void BindData() { //============================Query For Master Rows========================= string QueryString = "SELECT OrderID, CustomerID, EmployeeID, ShipName, " +
"ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry FROM Orders";
//==========================================================================
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
try
{
conn.ConnectionString = connectionstring;
if ( conn.State == System.Data.ConnectionState.Closed )
{
conn.Open();
}
System.Data.SqlClient.SqlDataAdapter adapter =
               new System.Data.SqlClient.SqlDataAdapter( QueryString, conn);
DataSet ds = new DataSet();
adapter.Fill( ds );
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
catch (Exception ex1)
{
Response.Write("An Error has occurred. <BR/>");
Response.Write(ex1.Message.ToString());
    Response.End();
}
finally
{
    if ( conn.State == System.Data.ConnectionState.Open )
{
conn.Close();
}
}
}
//Generic Method to a query and return a dataset. private DataSet RunQuery(string QueryString)
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
try
{
conn.ConnectionString = connectionstring;
if ( conn.State == System.Data.ConnectionState.Closed )
{
conn.Open();
}
System.Data.SqlClient.SqlDataAdapter adapter =
                     new System.Data.SqlClient.SqlDataAdapter( QueryString, conn);
DataSet ds = new DataSet();
adapter.Fill( ds );
return ds;
}
catch (Exception ex1)
{
Response.Write("An Error has occurred. <BR/>");
Response.Write(ex1.Message.ToString());
Response.End();

//This line below will never execute. return null;
}
finally
{
if ( conn.State == System.Data.ConnectionState.Open )
{
conn.Close();
}
}
}

//Control the child details by getting information from datagrid cells. private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
string DetailsQuery = "SELECT C.CompanyName, OD.UnitPrice, " +
                    "OD.Quantity, OD.Discount, E.FirstName, E.LastName "
+
"FROM [Order Details] as OD, [Customers] as C, [Orders] as O," +
                   "[Employees] as E "
+
"where OD.OrderID = O.OrderID and O.CustomerID = C.CustomerID " +
                    "and O.EmployeeID = E.EmployeeID and "
+
"O.OrderID = '" + e.Item.Cells[1].Text + "' and " +
"C.CustomerID = '" + e.Item.Cells[2].Text + "' and " +
"E.EmployeeID = '" + e.Item.Cells[3].Text + "'";



   //Creating a new datagrid to show the details of this item.     //You can also use any other control you want, or write html manually.     //For this example, I am using a simple new datagrid to bind the child row data. DataSet ds = this.RunQuery(DetailsQuery);
DataGrid NewDg = new DataGrid();
NewDg.AutoGenerateColumns = true;
NewDg.Width = Unit.Percentage(100.00);
NewDg.DataSource = ds;
NewDg.DataBind();

//Set up how the details datagrid will look like. SetProps(NewDg);
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
NewDg.RenderControl( htw );


string DivStart = "<DIV id='uniquename" + e.Item.ItemIndex.ToString() +"' style='DISPLAY: none; HEIGHT: 1px;'>";
string DivBody = sw.ToString();
string DivEnd = "</DIV>";

string FullDIV = DivStart + DivBody + DivEnd;


int LastCellPosition = e.Item.Cells.Count - 1;
int NewCellPosition = e.Item.Cells.Count - 2;

e.Item.Cells[0].ID = "CellInfo" + e.Item.ItemIndex.ToString();

if ( ViewState["Mode"] != null && ViewState["Mode"].ToString() == "ShowDetails")
{
if ( this.txtExpandedFields.Text.IndexOf(e.Item.Cells[0].ClientID) != -1 )
{
//make it expand. FullDIV = FullDIV.Replace("DISPLAY: none","DISPLAY: block");
e.Item.Cells[0].Text = "<A>-</A>";
}
}

if (e.Item.ItemType == ListItemType.Item)
{
e.Item.Cells[LastCellPosition].Text = e.Item.Cells[LastCellPosition].Text +
"</td><tr><td bgcolor='f5f5f5'></td><td colspan='" +
NewCellPosition +"'>" + FullDIV;
}
else
{
e.Item.Cells[LastCellPosition].Text = e.Item.Cells[LastCellPosition].Text +
"</td><tr><td bgcolor='d3d3d3'></td><td colspan='" +
NewCellPosition +"'>" + FullDIV;
}

//============Set up javascript methods.============= e.Item.Cells[0].Attributes["onclick"] = "HideShowPanel('uniquename" +
                        e.Item.ItemIndex.ToString() + "'); ChangeHLText('" +
e.Item.Cells[0].ClientID + "'); SetExpanded('" +
                        e.Item.Cells[0].ClientID + "','" +
txtExpandedFields.ClientID + "');";

e.Item.Cells[0].Attributes["onmouseover"] = "this.style.cursor='hand'";
e.Item.Cells[0].Attributes["onmouseout"] = "this.style.cursor='hand'";
}
}

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//clean up expanded records. txtExpandedFields.Text = "";
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindData();
}

public void SetProps(System.Web.UI.WebControls.DataGrid DG)
{
DG.Font.Size = 8;
DG.Font.Bold = false;
DG.Font.Name = "tahoma";

/*******************Professional 2********************
//Border Props
DG.GridLines = GridLines.Both;
DG.CellPadding = 3;
DG.CellSpacing = 0;
DG.BorderColor = System.Drawing.Color.FromName("#CCCCCC");
DG.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1);


//Header Props DG.HeaderStyle.BackColor = System.Drawing.Color.SteelBlue; DG.HeaderStyle.ForeColor = System.Drawing.Color.White; DG.HeaderStyle.Font.Bold = true; DG.HeaderStyle.Font.Size = 8; DG.HeaderStyle.Font.Name = "tahoma";
DG.ItemStyle.BackColor = System.Drawing.Color.LightSteelBlue;
}

------------------------------------------------------------------------

This article exposes the ease of implenting div in the code, and using client side
script to show/hide information. It is more simpler to do then most would think. 
The key point is that once a datagrid is generated, each column endswith the td tag. 
You can force html after the td tag to create a new row holding the div with your
detailed information.  This can be seen in the Item Databound method.

 

Notes

The code was done in VS2003 with Framework 1.1. I did not use VS2005 with GridView, though it can be easily modified.
 

Tags

 

Ratings

Was this article helpful? Don't forget to rate it. Ratings helps community members identify top & useful articles.
Current Rating: rating starrating starrating starrating starrating star
Rate this article:
(1-Poor, 2-Needs Improvement, 3-Average, 4-Good, 5-Excellent)
 

Feedback

Have a question, suggestion or feedback for this article? Leave your comments below.
Share your feedback:
Need to upload images with your comment. Upload your images here: Image Shack. Remember to copy the url of your uploaded image and insert it into the comment.
You can insert images by clicking on the following icon: insert comment image
(Members Only)
 

Latest Comments

Below are the latest comments from other ProgTalk members.
Posted on:
7/6/2010 3:45:46 AM
Posted by:
This is very helpful. How is it possible to have a expand/collapse ALL function?
Posted on:
4/27/2010 5:03:33 AM
Posted by:

details on how to put a textbox with value?

Posted on:
4/5/2010 6:23:09 AM
Posted by:

Can we add image instead of + and - text to expand and collapse here?

Posted on:
2/3/2010 12:55:09 PM
Posted by:
How to add edit,delete,update image buttons for both the master grid n child grid can u please show me the code for it????

Posted on:
11/3/2009 9:37:07 AM
Posted by:

This is a great article. This solved all my problems in my project.

Posted on:
6/2/2009 12:00:00 AM
Posted by:
do
It works great in I.E. However, the products list (child) runs over the categories (parent) list in Firefox. Please suggest.
Posted on:
6/2/2009 12:00:00 AM
Posted by:
do
I apologize, I just noticed your ealier post on this. I change height to auto and now it's working fine in Firefox.

at this line   "string DivStart"   in default.aspx.cs page.
Posted on:
5/28/2009 12:00:00 AM
Posted by:

I''m having a small issue I''m hoping you can explain for me.
I want to test each "master" record that is returned from my DB and then color the row''s accordingly (whether despatched or not). So I''ve added a Template Column to the "master" grid with a Label, and then get the value of the label and do my test and assign the row color accordingly (in the databind before anything else). When the template column is in the master grid, doing this somehow causes the javascript to fail. The "+-" click doesn''t work.
When I debug I can see that without the Template Column the code works through and adds the <Div> info after each child row, but with the Template Column in there it works through the databind OK and then steps out to the aspx page where the template column is, and when I view my source, the "<div>" is not there??
Javascript is not my forte so I''m hoping its something small and bizarre that all you guru''s know. I am using Master Pages which hasn''t been an issue.
Thanks for you help and good work on the grid. Very nice!

Posted on:
5/29/2009 12:00:00 AM
Posted by:

this may be a work around, but may work for you.  create a dumby column on the grid as the last column.  Also, I believe the html that gets spit out may not be correct.  I am working on a new version which will be much more cleaner.  This should help resolve your issue.

Rajib

Posted on:
5/4/2009 12:00:00 AM
Posted by:

Hello,
I''ve created a Web Custom Control from this example but I have a bug I need some help troubleshooting.

The grid works great in both standard asp.net (2.0) pages and master/child pages. I didn''t have a problem with the control ID when using master pages as Elham had but I have another problem.

Bug: The grid works unless I have 2 grids on the same page, then they do not expand. But if I remove one they expand. This is for both standard aspx pages and when using master pages.

Any ideas/suggestions?

Posted on:
5/29/2009 12:00:00 AM
Posted by:
Hi Chance,

This happens as the child div id's are teh same on both the grids.  if you make them changed, and modify the javascript, everything should be fine.  Let me know if this will work for you.

Rajib
Posted on:
6/1/2009 12:00:00 AM
Posted by:
Yeah, that was it Rajib. I've changed the Div IDs and it works. I have one other issue I'd like to troubleshoot as well.
How do we make it compatible with FireFox?
Posted on:
4/29/2009 12:00:00 AM
Posted by:
The HGrid is really good , thanks for sharing this . Its really good.
Posted on:
5/29/2009 12:00:00 AM
Posted by:

Hi Viswas,

Thanks for the great feedback.

Posted on:
12/26/2008 12:00:00 AM
Posted by:
Hi Rajib
Thanks for the answer, I ''ll try it first thing on Monday. I have another question: In the form that I have got user should be able to edit the values showed in nested grid and then save it. Is it doable in this sample or I have to think of using another control?

Regards
Ellie
Posted on:
12/24/2008 12:00:00 AM
Posted by:
Hi
Thanks for the article. I downloaded the source code and followed the step which mentioned. But I év got an issue with that!!!
when calling HideShowPanel function in javascript in following code:

e.Item.Cells[0].Attributes["onclick"] = "HideShowPanel(''uniquename" +  e.Item.ItemIndex.ToString() + "''); ChangeHLText(''" +
                        e.Item.Cells[0].ClientID + "''); SetExpanded(''" +  e.Item.Cells[0].ClientID + "'',''" +   txtExpandedFields.ClientID + "'');";
It passes parameter correctly but in javascript var ChosenPanel = document.getElementById(Panel); returns null!!!
I have got a master page & the grid is inside content. Any idea why it is like that?

Also I need to create a row in data grid with apply button:   <tr>... header item...</tr><tr>.. default values..</tr><tr>...apply button for each column...</tr> <tr> other rows...</tr> is it doable in this nested grid & if yes how can i write code for those buttons? I need to apply default values on other rows (same column) when user click on each apply button.

Thanx
Ellie
Posted on:
12/25/2008 12:00:00 AM
Posted by:
Hmm...when you use master pages, usually it changes teh id of the controls.  Launch your page, and do a view source,  Then search for uniquename, and see how the id is generated.  It probably is something like Master1_uniquename0 or something.  Once you find the pattern, just update the e.Item.Cells[o].Attributes["onclick"] line above to proper id.  This should resolve your problem.  The apply button, will it be in the child row of master row?  Master row is easily doable, and the so is the childrow, but you have to create a dynamic event.  let me know if this works for you.

Rajib
Posted on:
12/9/2008 12:00:00 AM
Posted by:
So far I''ve LOVED the hgrid until I encountered a bit of a problem with button columns.

First of all, in ASP.net 2.0 (with standard GridView controls) theirs apparently a .net bug with image button columns. So I found a work around with GridViews as follows:

            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                <asp:ImageButton runat=server id="EditButton" CommandName="Edit"
                ImageUrl="~/_public/images/edit.gif" CommandArgument=''<%# DataBinder.Eval(Container,
                "RowIndex") %>'' OnCommand="EditCol_Click" />
                </ItemTemplate>
            </asp:TemplateField>

But then I had to scrap the GridView control b/c I need an expandable grid. Thus my interest in the hgrid. With the hgrid I apparenlty can''t use the Imagebutton column. Thats fine... I''ll just use a standard button column.
Now I have an even bigger problem:

I want the last column (of the "Master" row) to be the edit button column and I''ve had 2 problems with this.
 1) If the last column of the Master columns is a button column, the button is not visible. =/  As long as its not the last column its fine. But I need it to be the last column.

2) I thought, well, what if I add another column after the button column (Thus making the button column 2nd from last) and set the visible property of that last ''dummy'' column to  false then the button will display properly. Wrong! If the last column is not set to visible then the "Slave" columns are never visible.

So I can have one or the other but not both. I can have either the visible edit button column (as the last column) or I can have the slave row.

Has anyone else had this problem? How can I fix this?
Posted on:
12/6/2008 12:00:00 AM
Posted by:
Posted on:
9/24/2008 12:00:00 AM
Posted by:
i had a problem here where i only can click on 1 row to view the child info but when i click on second row, the child won''t displayed..
Posted on:
12/6/2008 12:00:00 AM
Posted by:
Thanks for the its much appreciated


<a href="http://www.oyunim.com" title="oyunlar">çocuk oyunlari</a> <a href="http://oyunara.net" title="oyunlar">oyunlar</a>
Posted on:
9/24/2008 12:00:00 AM
Posted by:
i had a problem here where i only can click on 1 row to view the child info but when i click on second row, the child won''t displayed..
Posted on:
8/9/2008 12:00:00 AM
Posted by:
hi..
Could you do this code for Visual studio 2005 with Gridview...
from yesterady i am trying to make it work with gridview
and i did try to change at lot many places but i am getting few minor error becoz of javascript i think so.
as i didnt want the button and messages as i jus wanted to display the information

Thanks.
Posted on:
9/11/2008 12:00:00 AM
Posted by:
Posted on:
5/21/2008 12:00:00 AM
Posted by:
Hi,

Im trying to download the source code but have been redirected to this same page everytime.
could you please provide me the link to get the source code
Posted on:
5/21/2008 12:00:00 AM
Posted by:
Once you are logged in, click on the [Download Source Code] on the top of the article page.  It should automatically let you download it.  If you are not logged in, I think it redirect you to the login page so you can login.  Once you login, it redirect you to the article page.  Just click on the download source code, and it should work.  I just tried it myself.
Posted on:
5/19/2008 12:00:00 AM
Posted by:
Good Article. I have a problem.

An error has occurred: Error mientras se establecía la conexión con el servidor. Al conectar con SQL Server 2005, el error se puede producir porque la configuración predeterminada de SQL Server no admite conexiones remotas. (provider: Proveedor de canalizaciones con nombre, error: 40 - No se pudo abrir una conexión con SQL Server)

I hope this helps.

thanks.
Posted on:
5/19/2008 12:00:00 AM
Posted by:

Asegúrese de que su cadena de conexión es correcta. Si tiene SQL Server 2005, con el modo mixto, debe tener un userid y contraseña. Sobre la base de esa información, modificar la siguiente línea de código:

private string connectionstring = "server=SERVERNAME;database=Northwind;uid=USERNAME;password=PASSWORD;";

con sus credenciales ...

Rajib

Posted on:
4/8/2008 12:00:00 AM
Posted by:
Great article!  Anyway, I also need editing feature. I can added Edit,Cancel,Update,Delete & Insert row into a master table and do database handle accordingly, But How to added those feature into detail table that was handled locally by javascript? Please advice.
Posted on:
4/12/2008 12:00:00 AM
Posted by:

I see you discovered the flaw of the hgrid.  =) 
There are actually two ways of handling this.  The first way is the simple of two, but something you may not prefer.  Usually I just add a hyperlink in the child rows which passes the id to another page where they can edit the child record.

The more stylish way and second way is to use a div that is in absolute position with style attribute as:

<div id="DivChildDetails" style="display: none; visibility: visible"></div>
along with the absolute width, position, height, etc...

This way when your user clicks on edit from the child rows, do the following:
1. use some javascript to show the div ( this.style.display = "block";)
2. Load a table with all the items from the child grid (You can use javascript for this too)
3. Let them make their changes, and hit a submit button.

On the submit button you can update your record, and the current page will automatically change with the new results.

Let me know if this helps.

Posted on:
3/5/2008 12:00:00 AM
Posted by:
Good Article. I have a problem. The child row has an Edit which has a mappingid passed. So for the same the dataset ds had to have the Mappingid . But i dont want to see it displayed in the Child grid. How can i do that ?

NewDg.DataSource = ds;
NewDg.DataBind();



- fiat trading 1 1 -1
  Environment Reserved NTUser Server StartDate EndDate Comments MappingID ClearCaseBranch
Edit DEV No intranet\soundraj 1/1/9999 12:00:00 AM 1/1/9999 12:00:00 AM   28  

Thanks
Rajaraman.S
Posted on:
3/5/2008 12:00:00 AM
Posted by:
If you want to hide the MappingID column, try doing the following before you bind the child dataset:

if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{

    int MappingIdColumnPosition = 2;
    if (ds.Tables[0].Columns.Count >= MappingIdColumnPosition)
    {
         ds.Tables[0].Columns.RemoveAt(MappingIdColumnPosition);
    }
}

NewDg.DataSource = ds;
NewDg.DataBind();


Posted on:
2/18/2008 12:00:00 AM
Posted by:
I have having trouble connecting the Northwind database to this project. Can you tell me how to connect it to this project? I am using Visual Web Studio Express 2008. Thanks.
Posted on:
2/18/2008 12:00:00 AM
Posted by:
Is it easy to change from the Northwind database to mysql? How would I go about doing this? I am a newbie at this. Thanks.
Posted on:
2/18/2008 12:00:00 AM
Posted by:
Brian,

To connect to MySQL, you need to have the MySQL  Connector/NET.  If you have this I think the following lines can help you to connect to the the database
( MySql.Data.MySqlClient )

MySqlConnection cnn = new MySqlConnection("Network Address=ServerNameItMayBelocalhost;" +
"Initial Catalog=''DBName'';" +
"Persist Security Info=no;" +
"User Name=''YourUserID'';" +
"Password=''YourPassword''");
//Then you can open or close
cnn.Open();
cnn.Close();

I hope this helps.
Posted on:
2/18/2008 12:00:00 AM
Posted by:
Brian,

If you would like to connect to the Northwind database, on a SQL server, you need to open the Default.aspx code behind page (Default.aspx.cs), and look for the following text on the top of the page:

private string connectionstring = "server=SERVERNAME;database=Northwind;uid=USERNAME;password=PASSWORD;";

Here, just modifiy the SERVERNAME, to your server name.  It could be local if it is on your local machine.
Next change the USERNAME to your user id which logs into the database server. 
Finally you need to change the PASSWORD to your password which the user id needs to log in.

If you are using Windows Authentication, I guess you can pass your credentials, or create a new user which has access.  I am using SQL Server 2005 Express edition, but I am pretty sure it should be similar.

I hope this helps.
Posted on:
1/14/2008 12:00:00 AM
Posted by:
Where is the source code for hGrid.dll?
Posted on:
1/14/2008 12:00:00 AM
Posted by:
There is a [download source code] button on the top of this page.  Just sign on, and click that, and you should have the full source code.  If you are thinking that the Hgrid.dll is seperate code, it is not.  It was just the name of the project which I built, so it created a dll in the bin folder with the same name.  Hope this helps.
Posted on:
1/14/2008 12:00:00 AM
Posted by:
 
That is correct.  Hgrid has no special code.  The entire source code is here to download.
Posted on:
1/10/2008 12:00:00 AM
Posted by:
How do I add another hierarchy?

- Group 1
   - Sub Group 1
      My final Data
      My second final data
   + Sub Group 1, 2
+ Group 2
Posted on:
1/10/2008 12:00:00 AM
Posted by:
Wow, thats a tough one.  You will have to modify the DataGrid1_ItemDataBound method. 
I am not sure if you can enable collapse and expand method though.  I tried on my side, but
kept on getting some errors of how the html formed.  Here is what I have done so far though,
Just modify the Item_Databound event:

NewDg.DataSource = ds;
NewDg.DataBind();

//Get all rows of child grid and add inner contents
//You can use the new row of data where I wrote ''Some Data''

for ( int i = 0; i < NewDg.Items.Count; i ++ )
{
                int lastchildcell = NewDg.Items[i].Cells.Count - 1;

NewDg.Items[i].Cells[lastchildcell].Text = "</td></tr><tr><td colspan=''" +

                NewDg.Items[0].Cells.Count.ToString() +

"''><div style=''DISPLAY: block;'' id=''InnerDiv" +

 i.ToString() + "''>" + "-->Some Data<BR/>-->Some More Data<BR/>" +

"</div>";

}

                                                               
SetProps(NewDg);


Posted on:
12/20/2007 12:00:00 AM
Posted by:
I also just realized, that if you are using IE 7 or firefox, in the item databound, make sure 
the div that is created does not have an height = 1px, but rather nothing or auto.
This way it is properly displayed throughout all browsers.
Posted on:
4/23/2012 2:56:18 PM
Posted by:
I was seeking and seeking why it collapse to another rows... and I've  finally red then comment.... Thanks God!!!
Posted on:
12/20/2007 12:00:00 AM
Posted by:
Excellent post!!! Thnkxxx
Posted on:
11/28/2007 12:00:00 AM
Posted by:
Hi, In the inner grid, I added a drop down and the values needs to be saved in the database. Can you please help as how to achieve that?
Posted on:
12/1/2007 12:00:00 AM
Posted by:
One way would be to do it in Javascript.
Add a html button where the
Posted on:
11/13/2007 12:00:00 AM
Posted by:
Looks like there is a small bug.. if you click on the post back button from the bottom and from then on the "+" and "-" does not work correctly.
Posted on:
11/24/2007 12:00:00 AM
Posted by:
Hey Vijay, I'll try to fix that bug.
Posted on:
11/12/2007 12:00:00 AM
Posted by:
good work, I am already yours :)
Posted on:
11/12/2007 12:00:00 AM
Posted by:
typo...I'm already using yours :)
Posted on:
10/17/2007 12:00:00 AM
Posted by:
very useful
Posted on:
9/16/2007 12:00:00 AM
Posted by:
Never thought of this approach. Nice one.