search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Resource Tree from Database

Asked by Guss Davey
8 years ago.

I could not find any code in the forums building a tree from the database using id, parentid. (So I had to think myself :-(

Here is how I did it.

private void loadResources()
{
SqlDataAdapter da = new SqlDataAdapter("SELECT Convert(varchar(10),[ID]) as ID, [Name], [ParentID] FROM [vtPlaces]", ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
if (dr["ParentID"] == DBNull.Value)
{
DayPilot.Web.Ui.Resource res = new DayPilot.Web.Ui.Resource((string)dr["Name"], (string)dr["ID"]);
res.ChildrenLoaded = true;
DayPilotScheduler1.Resources.Add(res);
res.Expanded = true;
foreach (DayPilot.Web.Ui.Resource cres in GetChildResources(Convert.ToInt32(dr["ID"]),dt))
{
res.Children.Add(cres);
}
}
}
}

private DayPilot.Web.Ui.ResourceCollection GetChildResources(int id, DataTable DataSource)
{
DayPilot.Web.Ui.ResourceCollection childresources = new DayPilot.Web.Ui.ResourceCollection();
DataView dataView1 = new DataView(DataSource);
String strFilter = "" + "ParentID" + "=" + id.ToString() + "";
dataView1.RowFilter = strFilter;

if (dataView1.Count > 0)
{
foreach (DataRow dr1 in dataView1.ToTable().Rows)
{
DayPilot.Web.Ui.Resource cres = new DayPilot.Web.Ui.Resource((string)dr1["Name"], (string)dr1["ID"]);
foreach (DayPilot.Web.Ui.Resource cres2 in GetChildResources(Convert.ToInt32(dr1["ID"]), DataSource))
{
cres.Children.Add(cres2);
}
childresources.Add(cres);
}
}
return childresources;
}

This question is more than 3 months old and has been closed. Please create a new question if you have anything to add.