Hi all, i?ve going completely mad during all morning trying to figure out how to populate a treeview with data from a dataset, and i?ve get very far, but im stuck with an error i cant understand, so maybe you ppl can give me a hand on this one:
Im working with dnn, wich will explain any "bizarre development behavior", but the issue im confronting its mainly the treeview control (thats why i post my trouble here).
Have a SP wich returns a dataset, wich has 4 columns, all strings, the thing its that i have the 2nd column being the parent elements of the 3rd column (Ej: Providername(Column2) ArticleName(Column3)) so the values on the column 2 get repeated N number of times (one for each article that that provider provides).
This is the approach im working on:
protected void Btn_Click(object sender, EventArgs e)
{
ObjectDataSource Cat2=new ObjectDataSource();
Cat2.SelectMethod="GetCatalogoDS";
Cat2.DataObjectTypeName = typeof(CatalogoInfo).FullName;
Cat2.TypeName = typeof(CatalogoController).FullName;
Cat2.SelectParameters.Add("FamiliaInicial", "10");
Cat2.SelectParameters.Add("FamiliaFinal", "20");
DataView dv = new DataView();
dv = (DataView)Cat2.Select();
DataTable dt = dv.ToTable();
PopulateNodes(dt, TreeView1.Nodes);
}
private void PopulateNodes(DataTable dt, TreeNodeCollection tree)
{
string backup = " ";
TreeNode tn = new TreeNode();
foreach (DataRow dr in dt.Rows)
{
if (backup != dr.ItemArray[0].ToString())
{
tn.ChildNodes.Clear();
tn.Text = dr.ItemArray[0].ToString();
tn.Value = dr.ItemArray[0].ToString();
tree.Add(tn);
backup = tn.Text;
TreeNode tc = new TreeNode();
tc.Text = dr.ItemArray[3].ToString();
tn.ChildNodes.Add(tc);
}
else
{
TreeNode tc = new TreeNode();
tc.Text = dr.ItemArray[3].ToString();
tn.ChildNodes.Add(tc);
}
}
And it works... but with only one parent node, the moment i try to add a new node (tree,add(tn))i get a :
System.ArgumentOutOfRangeException was caught
Message="Index must be within the bounds of the List.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.Collections.Generic.List`1.Insert(Int32 index, T item)
at System.Web.UI.WebControls.TreeNodeCollection.AddAt(Int32 index, TreeNode child)
at System.Web.UI.WebControls.TreeNodeCollection.Add(TreeNode child)
at YourCompany.Modules.Catalogo.ViewCatalogo.PopulateNodes(DataTable dt, TreeNodeCollection tree) in c:\Inetpub\wwwroot\DotNetNuke\DesktopModules\Catalogo\ViewCatalogo.ascx.cs:line 207
at YourCompany.Modules.Catalogo.ViewCatalogo.BtnBicicletas_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\DotNetNuke\DesktopModules\Catalogo\ViewCatalogo.ascx.cs:line 152
The code very much explains itself.... i puit the dataset on a datatable and pass it to the PopulateNode procedure, in it i cicle the datatable and for each row, i check the parent node, if it is the first time a parent node appears, i name it and attrach the first child node (cause they belong to the same row), from that moment on, i just add child nodes until the parent node changes...(and in that moment i get the error).
Now, i cant put the treenode definition inside the if clause cause if i do so, i cant add the child node on the else clause, so basically im stuck:S
Any help will be more than welcome.
Tnks a lot for your time.
Farewell