Treestyleview in a datagridview?

Hi

I'm looking for advice on how it might be best to go about implementing a tree-view style structure in a datagridview.

Currently have an app that displays an array in a datagridview - I use a DGV as it makes displaying, sorting and selecting child's play.

The underlying data has changed to now support a sort of folder like string e.g. Project\subproject, to any depth and it would be nice to let the user have the option of a folder like view. where they could click to reveal sub-folders and items in the folder. 

Is this something that can be done within a DGV or do I need to change and start again with a treeview   (I had a quick experiment with a treeview but it didn't seem to support multi-line selection which I absolutely need)

Any advice greatfully received.

Thanks

Steve

 



Answer this question

Treestyleview in a datagridview?

  • zwang2

    I've had a play with your great examples but I confess to getting a bit lost.

    My tree structure is just like a directory structure where the nodes that have children have no other row information - finally you get to the 'file' data which has many cells filling the row.

    I'm struggling with accessing the cells from a selected row - if it's a node with children then I don't access cells as there aren't any. If it doesn't have children then its the actual data.

    I'm experimenting with the following that just tries to fill a couple of labels with cell data. But it doesn't work - it picks up different data according to whether or not rows above the selected line are expanded or not.

    //this doesn't quite work right - trying to work out how to access rows but not nodes!!!

    private void treeGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

    {

    TreeGridNode node = treeGridView1.Nodes[e.RowIndex];

    if (!node.HasChildren)

    {

    DataGridViewRow row = treeGridView1.Rows[e.RowIndex];

    if (row.Cells[2] != null)

    {

    label1.Text = row.Cells[1].Value.ToString();

    label2.Text = row.Cells[3].Value.ToString();

    }

    }

    else

    {

    label1.Text = "Node selected";

    label2.Text = node.HasChildren.ToString();

    }

    }

    I know I'm missing something simple, any pointers you could offer would be much appreciated.

    Thanks

    Steve


  • JM32714

    Looks like it will be perfect - I'll see how i can incorporate it.
    Many thanks
    Steve

  • hoabinh

    Works perfectly.
    I now have a simple test app that:
    fills the treegridview
    changes the folder icons as nodes expand and collapse
    fills labels with data from the grid according to the row you select
    and has expand all, collapse all, expand selected and collapse selected buttons.
    Thanks to your help I think I'm finally getting the hang of it.

    Happy to post it somewhere if you think it might be useful.

    Thanks again
    Steve

  • sbellware

    Check out my blog for a sample that combines a TreeView with a DataGridView -- I call it a TreeGridView: http://blogs.msdn.com/markrideout/archive/2006/01/08/510700.aspx < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"

     

     


  • Bryce Eddings

    Yes - this is where better documentation (or more samples) on my part would be helpful -- a node index and a row index do not correlate, except for the root nodes, so there is a helper method that goes from row index to node: GetNodeForRow(). You can pass in an actual row or a row index.

    Let me know if this works for you.

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"


  • ghenap

    Thanks for the quick response - I'll give it a try at a more civilized hour (in the UK)

    Cheers

    Steve


  • Treestyleview in a datagridview?