1

I found this C# answer on S.O., but can't seem to get this working: c# populate treeview from LINQ object

In my case, suppose I have a List(Of Report_Data) which, for example, looks like this:

Var1   Var2        
V1     Sub Item 1      
V1     Sub Item 2      
V1     Sub Item 3      
V2     Sub Item 1      
V2     Sub Item 2     
V3     Sub Item 1

And I'm looking to use LINQ to fill a treeview to look like:

V1 (CheckBox)
-------Sub Item 1  (CheckBox)
-------Sub Item 2  (CheckBox)
-------Sub Item 3  (CheckBox)
V2 (CheckBox)
-------Sub Item 1  (CheckBox)
-------Sub Item 2  (CheckBox)
V3 (CheckBox)
-------Sub Item 1  (CheckBox)

So, in my Treeview-filling routine, I create the following in-memory query:

    Dim GroupedReports = From Rpt As Report_Data In ReportsToBeProcessed
                         Group Rpt By Rpt.Var1 Into Group

And then I thought I could loop through the Groups and then the grouped objects to fill the treeview - something along the lines of:

    For Each Grp As Object In GroupedReports 
        ... Add Parent node ...
        For Each Rpt As Report_Data In Grp
            ... Add Child Node ...
        Next
    Next

Firstly, I don't know what data-type to use for my Grp variable and secondly it doesn't seem to be working...
How do I do this correctly?

Community
  • 1
  • 1
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • 1
    This post: http://codecorner.galanter.net/2010/01/10/using-linq-to-bind-flat-data-to-infragistics-ultrawebtree/ describes populating Infragistics UltraWebTree from ADO.NET datatable, but concept is similar, I beleive you can use similar recursive approach. – Yuriy Galanter Jan 02 '13 at 20:48
  • Thanks @Yuriy, that is a good resource for sure, but I'm still stuck with trying to do it via LINQ with grouping somehow and I can't figure that part out.... – John Bustos Jan 02 '13 at 20:53

1 Answers1

2

Something like this, you mean?

(sorry, never write VB anymore, so best I can do is C#)

var grped = 
    from report in reports
    group report by report.Var1 into grp
    select grp;
var treeView = new System.Windows.Forms.TreeView();
foreach(var grouping in grped)
{
    var nodeFor = treeView.Nodes.Add(grouping.Key);
    foreach(var item in grouping)
    {
        var subitem = nodeFor.Nodes.Add(item.Var2);
    }
}

Edit: The "group by" construct returns a set of IGrouping<TKey, TValue> - you can kind of think of this as a key-value pair, with the key being the thing you grouped by, and the value being all elements that matched that key.

Here is the VB.Net code I believe:

Dim grped = From report In reports
            Group report By report.Var1 into grp
            Select grp

Dim treeView as New System.Windows.Forms.TreeView()

For Each grouping In grped
    Dim nodeFor = treeView.Nodes.Add(grouping.Key)
    For Each item In grouping
        Dim subitem = nodeFor.Nodes.Add(item.Var2)
    Next
Next
Wade73
  • 4,359
  • 3
  • 30
  • 46
JerKimball
  • 16,584
  • 3
  • 43
  • 55
  • thank you... It worked... All I changed was I added in `Select Group` and removed the variable type for `grp` in my for loop in total... Thanks!! – John Bustos Jan 02 '13 at 21:02