vb.net - TreeView Group by root -
i'm trying achive simple here. want treeview groups root item children show under common root.
in example below have 4 students , 3 coaches. 2 of students belong same coach. want 2 students 2 show under same root rather creating doing. can this?
current code:
'//loads data treeview2.nodes.clear() dim connection oledbconnection = new oledbconnection("provider=microsoft.ace.oledb.12.0;data source=./academydatabase.accdb;persist security info=false;") dim command oledbcommand = new oledbcommand("select distinct coach, fullname student order coach") command.connection = connection command.connection.open() '//run command dim datareader oledbdatareader = command.executereader() '//run command dim rows integer = 0 while datareader.read() dim columns integer treeview2.nodes.add(datareader(0).tostring()) columns = 1 datareader.fieldcount - 1 treeview2.nodes(rows).nodes.add(datareader.item(columns).tostring()) next rows += 1 end while
current output:
try using dictionary keep reference of parent nodes using:
dim coaches new dictionary(of string, treenode) while datareader.read() dim coachnode treenode = nothing if coaches.containskey(datareader(0).tostring) coachnode = coaches(datareader(0).tostring) else coachnode = new treenode(datareader(0).tostring) coaches.add(datareader(0).tostring, coachnode) treeview2.nodes.add(coachnode) end if coachnode.nodes.add(datareader(1).tostring) end while treeview2.expandall()
Comments
Post a Comment