Monday, April 11, 2011

Treeview in C#.






protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateRootNode();
}
}
//Call the function with null argument

protected DataSet PDataset(string select_statement)
{
SqlConnection _con = new SqlConnection(connStr);
SqlDataAdapter ad = new SqlDataAdapter(select_statement, _con);
DataSet ds = new DataSet();
ad.Fill(ds);
_con.Close();
return ds;
}


private void populateRootNode()
{
SqlConnection con = new SqlConnection(connStr);
string sql = "select FoodCategoryID,FoodCategoryName,(select count(*) FROM FoodCategories WHERE ParentID=sc.FoodCategoryID) childnodecount FROM FoodCategories sc where ParentID IS NULL";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
string sql2 = "select FoodPackID,FoodPackName,(select count(*) FROM FoodPacks WHERE CategoryID=cat.FoodCategoryID) childnodecount FROM FoodPacks sc left join FoodCategories cat on cat.FoodCategoryID = sc.CategoryID";
SqlDataAdapter da2 = new SqlDataAdapter(sql2, con);
DataTable dt2 = new DataTable();

da.Fill(dt);
populateNode(dt, TreeView1.Nodes); //Function used to fill Populate the tree nodes.
//populateNode2(dt2, TreeView1.Nodes); //Function used to fill Populate the tree nodes.
}
private void populateNode(DataTable dt, TreeNodeCollection node)
{
int parentId;
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
string nodeid = dr["FoodCategoryID"].ToString();
string addnew = "Thêm mới";
//tn.Text = dr["FoodCategoryName"].ToString();
int subnode = int.Parse(dr["childnodecount"].ToString().Trim());
tn.Text = dr["FoodCategoryName"].ToString() + " (" + subnode + ")";
tn.Value = dr["FoodCategoryName"].ToString();
tn.NavigateUrl = tn.Text;
parentId = Convert.ToInt32(dr["FoodCategoryID"]);
node.Add(tn);
populatesublevel(parentId, tn);
//populatesublevel2(parentId, tn);
}
}
private void populatesublevel(int parentId, TreeNode parentNode)
{
SqlConnection con = new SqlConnection(connStr);
string sql = "select FoodCategoryID,FoodCategoryName,(select count(*) FROM FoodCategories WHERE ParentID=sc.FoodCategoryID) childnodecount FROM FoodCategories sc where ParentID=@parentID";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.SelectCommand.Parameters.AddWithValue("@parentID", parentId);
DataTable dt = new DataTable();
da.Fill(dt);
populateNode(dt, parentNode.ChildNodes);
}

It woking very goo :)

2 Comments:

  1. Duc Nguyen said...
    http://www.codeproject.com/KB/aspnet/DataTreeView.aspx
    Duc Nguyen said...
    protected void Page_Load(object sender, EventArgs e)
    {
    fillTree();
    }
    private void fillTree()
    {
    //string connectionstring = ConfigurationManager.ConnectionStrings["your connectionstring name"].ConnectionString;
    string connectionstring = ConfigurationManager.ConnectionStrings["RestaurantDBConnectionString"].ConnectionString;
    SqlConnection mycon = new SqlConnection(connectionstring);
    mycon.Open();
    SqlCommand mycmd = new SqlCommand("Select * from FoodCategories where ParentID is null", mycon);
    SqlDataReader dr = mycmd.ExecuteReader();
    mycmd.Dispose();
    string[,] ParentNode = new string[100, 2];
    int count = 0;
    while (dr.Read())
    {
    ParentNode[count, 0] = dr.GetValue(dr.GetOrdinal("FoodCategoryID")).ToString();
    ParentNode[count++, 1] = dr.GetValue(dr.GetOrdinal("FoodCategoryName")).ToString();
    }
    dr.Close();

    for (int loop = 0; loop < count; loop++)
    {
    TreeNode root = new TreeNode();
    root.Text = ParentNode[loop, 1];
    root.NavigateUrl = "#";


    SqlCommand childcmd = new SqlCommand("Select * from FoodCategories where ParentID='" + ParentNode[loop, 0] + "'", mycon);
    SqlDataReader childdr = childcmd.ExecuteReader();
    int count2 = 0;
    SqlConnection mycon2 = new SqlConnection(connectionstring);
    mycon2.Open();
    SqlCommand childcmd2 = new SqlCommand("Select * from FoodPacks where categoryID='" + ParentNode[count2, 0] + "'", mycon2);

    while (childdr.Read())
    {
    TreeNode croot = new TreeNode();
    ParentNode[count2, 0] = childdr.GetValue(childdr.GetOrdinal("FoodCategoryID")).ToString();
    ParentNode[count2++, 1] = childdr.GetValue(childdr.GetOrdinal("FoodCategoryName")).ToString();
    croot.Text = childdr.GetValue(childdr.GetOrdinal("FoodCategoryName")).ToString();
    croot.NavigateUrl = "#";
    root.ChildNodes.Add(croot);
    SqlDataReader childdr2 = childcmd2.ExecuteReader();
    while (childdr2.Read())
    {
    TreeNode croot2 = new TreeNode();
    croot2.Text = childdr2.GetValue(childdr2.GetOrdinal("FoodPackName")).ToString();
    croot2.NavigateUrl = "MenuItems.aspx?RestaurantID=1&FoodCategoryID=2&FoodPackID=3";
    croot.ChildNodes.Add(croot2);
    }
    childdr2.Close();

    }
    childdr.Close();

    TreeView1.Nodes.Add(root);

    TreeView1.CollapseAll();

    //TreeView1.Nodes.Add(root);
    //TreeView1.CollapseAll();
    }
    }

Post a Comment