Customizing the DataGridView to support expanding/collapsing (ala TreeGridView) - Mark Rideout's Blog - Site Home - MSDN Blogs

Customizing the DataGridView to support expanding/collapsing (ala TreeGridView)

Customizing the DataGridView to support expanding/collapsing (ala TreeGridView)

Rate This

One of the first things that I wanted to customize with the DataGridView is to make it support hierarchical data

If you read my first blog post you'll find out that I'm not a developer (anymore). Even though I'm not a developer I still like to take features and customize them to do something really cool. As far as the DataGridView goes, customizing it to support hierarchical data is a much larger task since the structure of the DGV doesn't lend itself to having different column sets, so, I decided I'd settled for a tree like structure.

Think of a TreeView combined with a ListView and that is basically what I wanted to go with.

NOTE: This code is not supported by Microsoft and is provided "as-is". This code it not meant to show "best practices", but just showing a concept.

NOTE: 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. Some people have been able to modify the code to run without Visual Styles. See this post for details.

Original Code: http://www.windowsforms.net/blogs/markrideout/treegridview.zip

Here is a picture:

Anyway. The basic part of creating a DataGridView that can expand and collapse is to dynamically add and remove rows. That was the easy part. To make this really usable and extendable, I decided to add a lot code and make this easier to use. Here are some details:

Design

I wanted to ensure that the design of the TreeGridView supported normal TreeView type properties and features, so creating necessary classes to create the “tree view” experience wa necessary (see object model for more details).

Custom Painting – Painting an image in a cell is easy, but ensuring that the text from the DataGridViewTextBoxCell didn’t overlap the image took a bit of work. Using the Padding feature of the DataGridViewCellStyle, I add padding to the left side of the cell to account for the text and the indentation. This padding affects the painting and behavior of the text box cell, so editing a text cell correctly positions the editing control.

Siting/Unsiting a node – Since I don’t want to set padding and styling except when a node is actually displayed. When the node is displayed or in the grid, it is sited. When the node is sited I set all the necessary properties.

Unbound – Since expanding and collapsing is based upon dynamically adding and removing rows from the grid, I decided that unbound mode would be the best way to go with this. I’ve hidden the “databinding” properties and the virtual mode property since this doesn’t support those features.

Edit Mode – One thing that I had to deal with is that double-clicking a cell enters edit mode. This double-click occurs regardless of the padding, so double-click on the +\- symbol causes the control to enter edit mode. Edit also enters if you single click on a cell that already has focus. So, to deal with this I turn edit mode to be enabled only through programmatic means. I have code to handle the F2 key to enter edit mode. There are other ways to solve this, but I went with the F2 approach.

Object model structure

TreeGridNode - Just like a tree view, I wanted to have the concept of a node. I made the nodes class derive from a DataGridViewRow since a node in the list is the same as a row, just with a bit more info.

Here are some properties:

Nodes – Again, like the treeview, a node has children, so there is a Nodes property that returns child nodes. One of the challenges in coding this is to know when a node is actually a row or when it is just a node. A node is a row when it is in the grid, otherwise it is just a node.

IsSited – A node is “sited” when it is contained in the grid as a row. The Sited property is true in this case. There are a set of protected virtual methods on the TreeGridView class (SiteNode and UnSiteNode).

ImageIndex – Image index for the node’s image. Only used when an ImageList is associated with the TreeGridView.

Image – Image associated with the node. Sets or gets the image. When an ImageList is associated with the TreeGridView and an ImageIndex is set then this returns an image from the ImageList. You can set the Image property to an image if you aren’t using an ImageList.

Cells – Returns the cells for the given node. This wasn’t easy to do since accessing the cells for a node (or row) when the node isn’t sited. Using the DataGridView’s CreateCells method I can get the correct cells collection when the node isn’t in the grid.

TreeGridCell/Column – This is a special DataGridView cell that derives from the DataGridViewTextBoxCell. The main thing that this custom cell class does is to customize the cell drawing to make it look like a tree node. That means that it draws the node’s image and the +/- icons and the tree lines. The custom cell also is where a node detects when you click the mouse to expand or collapse a node. NOTE: A lot more work can be done to correctly detect that the mouse is directly over the +/- image. Right now I’m not doing that.

TreeGridView – This class derives from the DataGridView control. Many things are done in this class. Nodes are sited/unsited in the grid as actual rows. Somce DataGridView Properties are hidden since they do not apply.

Here are some properties:

VirtualNodes – One of the common things done with a normal TreeView is to dynamically add child nodes when the user is expanding the parent. With the normal TreeView usres add temp child nodes to get the + sign and support expanding, then remove the temp node later. With the VirtualNodes property, the TreeGridView always displays a + sign next to a node, even if the node doesn’t have any children. Then, by handling the Expanding event you can dynamically add child nodes.

ImageList – ImageList associated with the TreeGridView

Nodes – Identifies the root nodes.

ShowLines – Determines if the TreeGridView shows lines between nodes.

CurrentNode – Identifies the node that has focus.

Here are some events:

Expanding – Again, like the treeview, this event occurs before a node is starting to expand. You can add nodes in this event to fill in more details of a child node collection. When the VirtualNodes property is true, the TreeGridView will display the node with a + sign next to it even when it doesn’t have any children. Handling the Expanding event is where you would dynamically add new child nodes.

Expanded – After a node is expanded.

Collapsing – Before a node is being collapsed

Collapsed – After a node has been collapsed.

I’ve included a simple test app that creates a news group reader looking list that supports expanding and collapsing.

Let me know what you think and how you've used this in your project!

-mark

  • is it possible to set true righttoleftlayout property of this component?

  • Thanks!!Now I have make my own gridview similar to you and its working!!!Nice Work!

  • Incredible work!!! congrats... now is it possible to add checkmarks to the tree? That would make this control a killing one!

  • Hi, Thanks for such a nice customize control. I used your control and its very help ful. Now what i want to add a data grid view as a child node in a current data grid veiw. Can you help me for that ? I'm very thankful to you

  • Added VisibleGridCell : DataGridViewButtonCell overloaded Paint to INVISIBLE the cell.Visible = false;

    namespace AdvancedDataGridView

    {

       public class VisibleGridCell : DataGridViewButtonCell

       {

           private bool m_IsVisible;

           public VisibleGridCell()

           {

               this.m_IsVisible = true;

           }

           public bool Visible

           {

               get { return m_IsVisible; }

               set { m_IsVisible = value; }

           }

           public override object Clone()

           {

               VisibleGridCell c = (VisibleGridCell)base.Clone();

               c.m_IsVisible = this.m_IsVisible;

               return c;

           }

           protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,

               int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue,

               string errorText, DataGridViewCellStyle cellStyle,

               DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

           {

               if (!m_IsVisible) return;

               base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue,

                   errorText, cellStyle, advancedBorderStyle, paintParts);

           }

           public TreeGridNode OwningNode

           {

               get { return base.OwningRow as TreeGridNode; }

           }

       }

       public class VisibleGridColumn : DataGridViewButtonColumn

       {

           public VisibleGridColumn()

           {

               this.CellTemplate = new VisibleGridCell();

           }

           public override object Clone()

           {

               VisibleGridColumn c = (VisibleGridColumn)base.Clone();

               return c;

           }

       }

    }

  • This is a great contribution. Thank you. I have your component running seamlessly in a rather complex application. However, I have a problema whe I add a second treegridview inside a the same tab control but in different tabs i get an error. Is there any limitation in the number of treegridview controls you can use?

    Thank you.

  • There isn't any limitation though what might be occurring is that the TreeGridView in the second tab is not being created visually yet or data hasn't been associated yet. Any initialization code where you are adding content to the TreeGridView on the invisible tab might error out.

  • Hi Mark,

    How can I check if a node is expended? Is there a IsExpanded property available?

    Thanks.

  • Hi Mark,

    Everything is working now. I have a question:

    is there a "expanded" property in your control? I would like to know if a node is expanded at run time.

    Thanks a lot.

  • There isn't a public IsExpanded property on TreeGridNode but it is easy to do since there is an internal IsExpanded bool value. Open up TreeGridNode.cs and change this line from internal to public:

           internal bool IsExpanded;

    new

           public bool IsExpanded;

    Note that you don't want to publically set IsExpanded to true but instead use the Expand() method.

    -mark

  • Hi Mark,

    Thanks for your excellent code. It helps me a lot but I have some problems, I am quite new in C#.

    I have several questions, I hope that you can help me again.

    1. There is no node .selectednode, but I also cannot remove the row, it will give me error. so Is there anyway to delete a node or a row?

    2. How to expand a particular node?

    3. How to expand and collapse all the nodes using code?

    Regards,

    Zena

  • Hi Mark, This is a great Control. Thanks for it. when i am using this i have a prblem in binding data to ComboboxColumn.  

    First colulmn is a Textbox and second is comboboxcolumn. Each Combobox will have different list based on Textbox Value.

    How we can acheive this? Which Event

    Thanks

    Raj

  • Set Edit mode to on enter:

    [your_instance_name].EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;

    Cheers!

  • The call to TreeGridCell.UpdateStyle() is very slow and with a grid with 150 rows it take 10s to display the grid. It's very very long, how i can optimize this ?

    Regards;

      Gilles

Page 16 of 16 (239 items) «1213141516
Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post