Monday, June 13, 2011

Linq to Bindingsource

Issue: I want to build a dynamic treeview that 1) only loads the children when a parent is selected. 2) displays how many children in the node name. 3) works with filters already performed against the bindingsource.

The ideal way (in my mind at least) was to run sql against the bindingsource, that way the filters would be maintained. I do know a bit of linq and really hoped there was a linq to bindingsource; I didn't find anything showing this.
In the past, I have changed the commandtext of the tableadapter itself, but as I implemented it, I really didn't want to go that route because it meant maintaining multiple dynamic queries and filters that had to match back to the bindingsource filters.
So, after hours of work I gave up for the weekend. As often happens, I started from scratch this morning and had an epiphany: the tableadapter.fill is filling a data table, surely I can linq to that. Sure enough, a simple linq query worked! Now I can run a distinct linq query and enumerate the results to build my tree nodes.