views:

495

answers:

3

I'm trying to hack together a datagridview in c# that can handle hierarchical data (currently just 2 levels) since I couldn't find a 3rd-party control that worked and looked exactly as I needed it to.

I've managed to get expand/collapse and global sorting working (sort by top level group, then sort within each group) but I want to add a feature where you can sort just within a top level group. so imagine a 2-column example, e.g.

    column1     | column2
    group 1     |  6
      group 1 a |  1
      group 1 b |  2
      group 1 c |  3
    group 2     |  15
      group 2 a |  4
      group 2 b |  5
      group 2 c |  6

I want the user to be able to double click the "15" cell, and have it sort just the "group 2 X" rows, with all other rows staying exactly where the are. each cell's tag tells it what it's parent is, so I know whether the given pair of rows are part of the same sub-group. I can't quite figure out the logic though. I tried setting SortResult = 0 for any pair of rows not in the same group, but I still have to handle cases where one row is in the subgroup and one isn't. any ideas?

+1  A: 

You can place a datagrid inside a column. I think this will achieve what you are aiming for

Sergio
it might, but I don't want to show another set of column headers if possible. I'll look into your idea though.
toasteroven
You can remove the headers on a datagrid. If you need more control you can use nested repeaters
Sergio
A: 

figured it out - I had an && condition where I should have had an ||... basically if the either of the pair of rows to be compared are not in the same subgroup, simply set SortResult to reflect which one is currently displayed before the other, making sure to flip the result depending on an ascending or descending sort. if both rows are within the same subgroup, then sort the as usual. now to clean up my messy code :)

toasteroven
A: 

Hi toasteroven,

I am interested in the way you handle hierarchical data in C# datagridview. Is it possible to show your code?

Thanks, Alvin