Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Sample Code: TreeGridView - collapsing/expanding DataGridView
 

Sample Code: TreeGridView - collapsing/expanding DataGridView

I've just started a blog and the first main post is a sample that I wrote to customize the DataGridView to support collapsing and expanding. I call it a TreeGridView: http://blogs.msdn.com/markrideout/archive/2006/01/08/510700.aspx

 

Let me know what you think!

 

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Monday, January 09, 2006 4:50 AM
Fantastic code Mark!! I have been looking and wondering if this was possible with the datagridview for sometime now. Thanks Alot!!!!
natetrost  Friday, January 13, 2006 6:29 PM

Sure thing! Let me know what cool UIs you make with it (if you can)!

 

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Friday, January 13, 2006 8:17 PM

Hi

Thanks for your coding.

But I got error when I opened the project.

My frmaework version is 2.0.50727

best regards

ZMT  Monday, January 16, 2006 11:32 AM

Can you post the error you got?

 

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Monday, January 16, 2006 5:49 PM

hi mark,

downloaded your code . got an error when i tried to run

visual styles-related operation resulted in an error because no visual style is curently active

drachx  Friday, January 20, 2006 12:23 AM

Yes sorry, this control is written to require Visual Styles to be enabled on your computer. You'll need to modify it if you want to run the TreeGridView without visual styles

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

Mark Rideout  Friday, January 20, 2006 1:25 AM

How I can change grouped column. In your example, it is "subject", I want to do it "From". I couldn't find a way to do it.

Could you please help?

JHG JetLane  Tuesday, January 24, 2006 12:38 AM

This example doesn't implement any grouping, it is a tree grid. That said, you can goto the Columns property in the property grid and in the columns collection editor change the Subject column type to be a DataGridViewTextBoxColumn and change From column to be a TreeGridColumn.

Next, you'll need to fix a bug in the TreeGridCode. Open the TreeGridNode.cs file and find the cells_CollectionChanged method.

Change this line of code

if (cell.GetType().IsAssignableFrom(typeof(TreeGridCell)))
to

if (typeof(TreeGridCell).IsAssignableFrom(cell.GetType()))
Note again, this won't get what you want -- you'll have to write a lot of code to change the data and node structure to do what you want. Open the Form1.cs file and you'll see that I've hard-coded the data to be based upon the subject.

Hope this helps
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

Mark Rideout  Tuesday, January 24, 2006 1:13 AM

Thanks Mark,

I know it is not exactly same thing, but it helps! Thank you again.

JHG JetLane  Tuesday, January 24, 2006 1:24 AM

is there a way to change column type programmatically instead of property grid?

Thanks !

JHG JetLane  Tuesday, January 24, 2006 2:03 AM

This is simply fantastic!!!.

Thanks for providing such a control.I have 1 question though , I would like to use it with edit mode and I have noticed that in your blog you say that you implemented the "F2" which works fine.However before reading your log I set the property "AllowUserTOaddRows =true " and it crashes.Any suggestions on this one?

Basically I would like to use it in a way that the user fills the tree and

whenever you start writing on a node another empty one is added.

Any suggestions?

Thanks a lot in advance

devBrix  Thursday, February 02, 2006 5:19 PM

I have found out that by setting AllowUserToAddRows to true it crashes and it looks like it crashes in the treeGridCell on line 186

"If (node._grid.SHowLInes) because is null.

Any ideas?thanks

devBrix  Thursday, February 02, 2006 6:33 PM

No ideas. I didn't design this to support allowing users to add rows (that is why it sets the value to false in the constructor).

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

Mark Rideout  Thursday, February 02, 2006 7:41 PM

fair enough.I really like your control,any advice on adding the ability to add rows?

thanks anyway for making this available

devBrix  Friday, February 03, 2006 6:42 AM

just what i was looking for.Awesome!!

I am new to the datagrid itself .I am struggling to make it work with a dataCombobox in it .Does anybody have an example how i can implement a combo in the treegridview?

THANKS

vbjunkie  Saturday, February 04, 2006 9:19 AM

Hi Mark

I can't show gird in DataGridView by set ShowLines property.

Please help me

Thanks

Steven

Steven Khiem  Friday, February 10, 2006 6:09 AM

ShowLines shows the tree lines. It doesn't change the cell's border options. You should set the CellBorderStyle property to something like Single.

-mark

DataGridView Program Manager

Microsoft

This post is provided "as-is"

Mark Rideout  Friday, February 10, 2006 6:53 PM

Mark,

Would be very grateful if you could show me how i can include a combobox with the treegrid.

this is the structure

City                                               

Hotel  Name  Star   Location

Star and Location should be comboBoxes. (1 star -2 stars 3 stars)  (Centre-Outskirt-airport etc)

London    

Hilton    4 star        Deluxe  

I have played quite a bit but i could not do it.

Can you help?

Thanks in advance

 

vbjunkie  Tuesday, February 21, 2006 10:59 PM

Just add a TreeGridColumn (Hotel), a TextBoxColumn (Name) and two ComboBox columns (Star and Location) to the dataGridView. Let me know if you have problems.

-mark

DataGridView Program Manager
Microsoft
This post is provided "as-is"

Mark Rideout  Wednesday, February 22, 2006 12:58 AM

HI Mark
I am Still having trouble in showing 2 combos.Can you help

I have set up a treegrid with 4 columns

Hotel TreeGridColumn
Name DataGridViewTextBoxColumn
StarRating DataGridViewComboBoxColumn
Location DataGridViewComboBoxColumn

For example sake I would like to fill the treegrid as follows:

Hotel

Hilton 4 star Central

Holiday Inn 4 star Airport


I have these 2 routines which loads the combos

private DataGridViewComboBoxColumn LoadLocation()
{

colLocation.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
colLocation.DataPropertyName = "Location";
colLocation.Items.AddRange(new string[] { "Centre", "Airport", "Station" });
colLocation.Sorted = true;
colLocation.SortMode = DataGridViewColumnSortMode.NotSortable;
colLocation.HeaderText = "Location";
return colLocation;
}
private DataGridViewComboBoxColumn LoadStarRating()
{
colStar.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
colStar.DataPropertyName = "Star";
colStar.Items.AddRange(new string[] { "1 Star", "2 Star", "3 Star","4 Star","5 Star" });
colStar.Sorted = true;
colStar.SortMode = DataGridViewColumnSortMode.NotSortable;
colStar.HeaderText = "Star Rating";
return colStar;
}


but i get lost when I need to load the grid

this does not work
private void LoadTreeGrid()
{
Font boldFont = new Font(treeGrid.DefaultCellStyle.Font, FontStyle.Bold);

TreeGridNode nodeHotel = treeGrid.Nodes.Add("Hilton",LoadStarRating(), LoadLocation());
nodeHotel.ImageIndex = 0;
nodeHotel.DefaultCellStyle.Font = boldFont;


}

I know it's wrong but I have tried many combinations.

can you help?
thanks again

vbjunkie  Wednesday, February 22, 2006 5:25 PM
You use VisualStyleRenderer to Draw Glypth sign (+ or -) flowing:
internal VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
internal VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
If OS haven't VisualStyle, what can you replace it?
maulanhonline  Monday, February 27, 2006 4:26 AM
Can you tell me about classes replace to draw sign (+/-) without visualstyle. Sorry, me is newbie
maulanhonline  Monday, February 27, 2006 4:45 AM

Has anybody managed to use the treegrid with some combos?

Is so could you post an example?

thanks

vbjunkie  Monday, February 27, 2006 6:18 AM

You can help me?

I have bug in your code in treegridview

I have to TreeGridView nameflowistree1 and tree2

my code:

TreeGridNode temp = tree1.CurrentNode;

Tree1.Nodes.Remove(temp);

Tree2.CurrentNode.Nodes.Add(temp);

if i run above code 2 time,

First, I add to Tree2.CurrentNode is node1 , it okie

Next, I add to Tree2.CurrentNode is node2, temp (now is child of node2) have level equal node2

maulanhonline  Tuesday, March 28, 2006 3:55 AM

I didn't develop the sample to support adding items, that is why you get the error.

-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"

Mark Rideout  Tuesday, March 28, 2006 4:15 AM

Hi, Mark

Thanks for your post.

I posted a Q yesterday :

I need to build a custom DataGridViewColumn with the cell contains a embedded DataGridView control.

I built one, it can display the “DataGridView�cell and works for edition but, when I click other cells, the DataGridView in the cell disappeared.

Please help, thanks!

Hans Guan

Hans Guan  Tuesday, March 28, 2006 11:16 PM

Mark,

The samples you provide require v2.0.50926 of the runtime - according to the error message when I attempt to execute the application.

How do I obtain this version? The .NET Framework site indicates that I have the most current version (2.0.50727) and doesn't provide a path to a newer one.

I'm looking forward to your control - it must surely beat the HierarchicalFlexGrid and the DataShape Provider of VB6.

Thank you,

Gus

augustus  Friday, April 07, 2006 6:03 AM

Great work, Mark!

When I add different levels to the TreeGridView, I need to be able to have different column types for the same column at different levels. Is there a way to dynamically change the column type (and EditingControl) when clicking on a particular cell in a column based on the level of the row that the cell is in? Thanks!

Myles R.

Myles R_  Thursday, April 13, 2006 3:14 PM
Using built in DataGridView functionality you can set a cell to a new cell type like so:

dataGridView1[2,3] = new DataGridViewImageCell();
This technique should work with this sample.
-mark
Program Manager
Microsoft
This post is provided "as-is"
Mark Rideout  Thursday, April 13, 2006 4:38 PM

Perfect. Thanks!

Myles R.

Myles R_  Thursday, April 13, 2006 8:04 PM

Has anyone been able to convert this to VB successfully at all.

So far I have been able to convert all classes but one (TreeGridNodeCollection.cs) and I would be apreciate any help on this because the methods for adding properties to the property grid are exactly what i am looking for, and i am interested in learning how it is done.

I learn fast from example

robnrie  Tuesday, April 18, 2006 2:48 AM

Hi Mark,

I have some truble converting a "normal" row to a TreeGridNode. Meaning if the row exist I cant seem to get it to be a TreeGridNode withoutthis causing a new row (TreeGridNode)to be added.

Any ideas?

Erik

Hyb  Wednesday, April 19, 2006 2:47 PM

Hi, thanx for your control ;)
i've a little question:
how i can retrieve cell value of child rows of a specific parent node?
i can retrive a cells values of parent node with this code:

treeGridView1.Nodes[ i ].Cells[2].Value.ToString();

but how i can retrieve cells values of child rows of a specific parent node?
sorry if it's a stupid question..

Raffaele 

dops  Sunday, April 23, 2006 10:02 PM
ok ok, i've resolved with a nested cicle :)

for (int i = 0; i < treeGridView1.Nodes.Count; i++)
{
MessageBox.Show(treeGridView1.Nodes[ i ].Cells[0].Value.ToString());
for (int j = 0; j < treeGridView1.Nodes[ i ].Nodes.Count; j++)
{
MessageBox.Show(treeGridView1.Nodes[ i ].Nodes[ j ].Cells[0].Value.ToString());
}
}

dops  Monday, April 24, 2006 8:56 AM

Mark

Some years ago I wrote an application in VS2003, ManagedC++, that uses a ContainerListView control that I picked up from one of the code sites. It works like a combination TreeView and ListView. I am now in the process of moving that application to VC2005 CLR/C++ and would like to use the TreeGridView in place of the ContainerListView control.

Initially, it seems to fit the bill. However, I can't get the DataGridViewCheckBox column to work. The CheckBoxes appear empty and will not show a checkmark when clicked (three state property set to false). The DataGridViewColumn seems to work fine in a normal DataGridView control. Any Suggestions or thoughts?

Thanks

John

Bollwerk  Tuesday, April 25, 2006 4:21 PM

Has anybody got a full example apart from the ones provided by the author of this control where it shows how to use the expand mode,use of comboBoxes and other controls.

Reading all the messages people post I think that everyone likes the control but we all in a way struggle to make it work they way it should or the author intended.

Any chance of a more in depth example?

thanks a lot

vbjunkie  Thursday, May 25, 2006 9:18 AM
Mark, have you done much on this control since your latest blog? Just wondering if you, or anyone else has implemented sorting? Sure would make life easier than having to figure it myself ;) hehe

Kris Wragg
Kris Wragg  Tuesday, June 20, 2006 12:13 PM

Hi,
has any one got the solution of the problem posted by vbjunkie. I am having the same problem. I have added the combobox but nothing is being displayed. Please help me for this.

ASIF

Asif Fayyaz  Monday, March 05, 2007 2:12 PM

Hi,
I am having problem with using combobox with treegridview. I have added the combobox with treegridview but its not working as dropdown button. No list is being displayed.

Inside the code, I am adding the node with the following code. Columns are in the following order:
Text, Text, Combo, number, Text

I am putting null value for combo. I have no idea what to send here. Even if I put some text here, then just that text is being displayed and no dropdown is working and nothing else is displayed.

AdvancedDataGridView.TreeGridNode node = treeGridView1.Nodes.Add("TextColumn1", "
TextColumn2", null, 1, "TextColumn3");

Combo box has the following items:
"None", "IncrBy", "Replace", "NextAvailable"

ASIF

Asif Fayyaz  Tuesday, March 06, 2007 4:29 AM

Mark

What do I have to do to get CheckBoxColumns working? Clicking on the CheckBox seems to have no effect, the check mark does not appear. I really want to use this control but need CheckBoxColumn functionality. Please respond.

Thanks

Bollwerk  Wednesday, May 16, 2007 12:55 PM
Never mind. I found what the problem is. To get CheckBoxCells (and ComboBoxCells, etc.) to work I must put them into edit mode. so in the CellClick event handler, when the ColumnIndex is that of a CheckBoxColumn, I set BeginEdit to true. It now works like a normal DataGridView control.
Bollwerk  Wednesday, May 16, 2007 3:24 PM

Hi

Thank you for your cool control, but I have a question

How to modify it to support right to left?

Dev_Moud  Friday, June 22, 2007 4:05 AM
Hey,

I was just wonder how I can change the height of a row in this control.

thanks
Vleermuis  Wednesday, July 04, 2007 10:06 AM
How do I get the current selected row?
fubak  Friday, September 14, 2007 12:40 PM

Mark,

the links to TreeGridViewposted on your blog seem to be dead.... Sad

Is the code still available? I would love to put my eye on it, please.

-PL

PL01  Saturday, October 06, 2007 3:19 PM

really a gud code

but plz help me

i m unable to convert it into vb.net

can u help me

because i have to made this application in vb.net

not in c#

kindly reply me fast

at

jain.mohit14@gmail.com

thanking you

jain.mohit14  Tuesday, December 11, 2007 2:07 PM

hello is there any asp.net version. Kind regards,
leoscorpio  Saturday, February 16, 2008 10:13 PM

Do you have meanwhile a version available without visual styles?

- ed

EB-549  Tuesday, July 29, 2008 9:50 AM
EB-549 wrote:

Do you have meanwhile a version available without visual styles?

- ed

I have also run into this problem, I was using the the assembly as a reference in another sample project and couldn't understand what I had done to change the code recently then it hit me I changed from the Windows XP theme.

mkbbrt  Thursday, August 28, 2008 1:23 PM
Making the TreeGridView work with Visual Styles disabled is quite simple:

-Find/create a plus and minus image (mine are 20x20 pixels)
-Add them to the resources of the TreeGridView project

Change the following in TreeGridView.cs
//internalVisualStyleRendererrOpen=newVisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
//internalVisualStyleRendererrClosed=newVisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
internalBitmaprOpen;
internalBitmaprClosed;
and add
ResourceManagerresourceManager=newResourceManager("AdvancedDataGridView.Properties.Resources",System.Reflection.Assembly.GetExecutingAssembly());
this.rOpen=(Bitmap)resourceManager.GetObject("minus");
this.rClosed=(Bitmap)resourceManager.GetObject("plus");
to the constructor.

In TreeGridCell.cs change the following:
if(node.HasChildren||node._grid.VirtualNodes)
{
//Paintnodeglyphs
//if(node.IsExpanded)
//node._grid.rOpen.DrawBackground(graphics,newRectangle(glyphRect.X,glyphRect.Y+(glyphRect.Height/2)-4,10,10));
//else
//node._grid.rClosed.DrawBackground(graphics,newRectangle(glyphRect.X,glyphRect.Y+(glyphRect.Height/2)-4,10,10));
if(node.IsExpanded)
graphics.DrawImage(node._grid.rOpen,newPoint(glyphRect.X-5,glyphRect.Y));
else
graphics.DrawImage(node._grid.rClosed,newPoint(glyphRect.X-5,glyphRect.Y));
}

HeikoHe  Wednesday, February 25, 2009 10:44 AM
Hey HeikoHe,

Have you come across the issue when on a treeNode.Remove() the .Index of the collection is messed up?

How did you fix that if so?
JeffMetzler  Friday, April 17, 2009 6:02 PM
Definately cool custom job. I wonder what would happen if you tried to sort on a column other than the one with the treeview in it. Have you tested this already? Or did you just block sorting when a column has a tree in it?
vbgroupie  Saturday, July 04, 2009 2:56 AM
Hi Mark i have your same problem with DataGridViewCheckBox in TreeGridView (your fantastic control)!!!

Have solved?

Help me please, my email is izumo82@hotmail.it

By
Izumo82  Tuesday, September 29, 2009 9:16 AM

You can use google to search for other answers

Custom Search

More Threads

• How Do I Format a DataGridView Column to datagridviewcheckbox Type.?
• Double Click Datagrid Row
• Formatting negative numbers as red in datagrid (event question)
• I can't see de Data menu, in order to add new data Source
• a question re:a simple datagrid..
• does it make a difference if the value given to CmbBOX.ValueMember is lower or upper case
• child DGV not updating when selection of parent DGV changes
• DatagridView - Cursor in the Last Column ?
• Binding custom objects to Data Grids
• cell error text clears when update Data Source