c# - Create Hierarchy with Linq -


i reading try , create hierarchy 2 levels deep in wpf, c# linq sql using 1 table. table not have parent or child key relationship. how can created in treeview?

http://www.scip.be/index.php?page=articlesnet09

here data db

i trying make paper 1st level , title second of tree.

dc12     once around          second around     new age#3     third around dc13     new age  dc14     rock & roll     rock & roll #2 dc15     top 5     new age #2 

here class'

public class parent {     public string paper { get; set; }     public ienumerable<child> readings { get; set; } }  public class child {     public string paper { get; set; }     public string title { get; set; }     public ienumerable<book> books { get; set; } } 

try simple code (sample project) :

1 .aspx

<table>    <tr>       <td>          <asp:treeview id="hierarchytreeview" expanddepth="3" populatenodesfromclient="true"                                                             forecolor="blue" backcolor="buttonface" showlines="true" showexpandcollapse="true"                                                             runat="server" ontreenodepopulate="hierarchytreeview_treenodepopulate" />       </td>    </tr>  </table> 

2 .cs

    protected void page_load(object sender, eventargs e){     this.populaterootlevel();     }      private void populaterootlevel()     {         datatable _datatable = this.getdatatable(applicationconfig.connstring, "sp_populaterootlevel", "parentid", string.empty);         this.populatenodes(_datatable, this.hierarchytreeview.nodes);     }      private void populatesublevel(string _parentid, treenode _parentnode)     {         datatable _datatable = this.getdatatable(applicationconfig.connstring, "sp_populaterootlevel", "parentid", _parentid);         this.populatenodes(_datatable, _parentnode.childnodes);     }      protected void hierarchytreeview_treenodepopulate(object sender, treenodeeventargs e)     {         populatesublevel(e.node.value.tostring(), e.node);     }      private void populatenodes(datatable _datatable, treenodecollection _nodes)     {         foreach (datarow _datarow in _datatable.rows)         {             treenode _treenode = new treenode();             _treenode.text = _datarow["empname"].tostring();             _treenode.value = _datarow["empnumb"].tostring();              if (_datarow["fgactive"].tostring() == "y")                 _nodes.add(_treenode);              _treenode.populateondemand = ((int)(_datarow["childnodecount"]) > 0);         }     }  public datatable getdatatable(string _prmconnstring, string _prmstoreprocedure, string _prmfield, string _prmvalue)     {         datatable _result = new datatable();          string[] _field = _prmfield.split('|');         string[] _value = _prmvalue.split('|');          try         {             sqlconnection _conn = new sqlconnection(_prmconnstring);             sqlcommand _cmd = new sqlcommand();             _cmd.commandtype = commandtype.storedprocedure;             _cmd.parameters.clear();             _cmd.connection = _conn;             _cmd.commandtimeout = 180;             _cmd.commandtext = _prmstoreprocedure;             (int = 0; < _field.count(); i++)                 _cmd.parameters.addwithvalue("@" + _field[i], _value[i]);              sqldataadapter _da = new sqldataadapter();             _da.selectcommand = _cmd;             _da.fill(_result);         }         catch (exception ex)         {         }          return _result;     } 

3 .sql

create proc dbo.sp_populaterootlevel(@parentid varchar(50))      select a.empnumb,a.empname,a.companyid, b.jobtitlename + ' - ' + c.joblevelname jobtitlelevel,   (select count(*) dbo.msemployee empnumbparent = a.empnumb) childnodecount,a.fgactive    dbo.msemployee    left join dbo.msjobtitle b on a.jobtitle = b.jobtitleid   left join dbo.msjoblevel c on a.joblevel = c.joblevelid   a.empnumbparent = @parentid 

Comments