是否可以在Treeview控件中显示员工详细信息

时间:2015-03-18 08:21:15

标签: c# asp.net treeview

我需要有关树视图控件的帮助。我在我的aspx页面中使用treeview控件,并使用存储过程从数据库绑定数据。我的输出很好,但我有一个问题,以显示树视图的员工详细信息。我的意思是如果用户单击树视图控件中的节点或值,我需要在树视图控件中显示所选值的详细信息。是否可以创建树视图的配置文件表..请帮我解决问题。

以下是我的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Username"] == null)
    {
        Session["Msg"] = "Your session has expired. please login again";
        Response.Redirect("../Account/login.aspx");
        //.........
        return;
    }


private void GetTreeViewItems()
{
    try
    {
       string cs = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
        SqlConnection con = new SqlConnection(cs);
        SqlDataAdapter da = new SqlDataAdapter("sp_GetEmployee", con);    
        DataSet ds = new DataSet();
        da.Fill(ds);

        ds.Relations.Add("ChildRows", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["ManagerId"]);
        foreach (DataRow level1DataRow in ds.Tables[0].Rows)
        {
            if (string.IsNullOrEmpty(level1DataRow["ManagerId"].ToString()))
            {
                TreeNode parentTreeNode = new TreeNode();
                parentTreeNode.Text = level1DataRow["FirstName"].ToString();
              // parentTreeNode.Text = level1DataRow["Designation"].ToString();
                parentTreeNode.Value = level1DataRow["id"].ToString();

                GetChildRows(level1DataRow, parentTreeNode);
                TreeView1.Nodes.Add(parentTreeNode);
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void GetChildRows(DataRow dataRow, TreeNode treeNode)
{
    DataRow[] childRows = dataRow.GetChildRows("ChildRows");
    foreach (DataRow childRow in childRows)
    {
        TreeNode childTreeNode = new TreeNode();
        childTreeNode.Text = childRow["FirstName"].ToString();
      // childTreeNode.Text = childRow["Designation"].ToString();
        childTreeNode.Value = childRow["id"].ToString();
        treeNode.ChildNodes.Add(childTreeNode);
        if (childRow.GetChildRows("ChildRows").Length > 0)
        {
            GetChildRows(childRow, childTreeNode);
        }
    }
}
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{

}


protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{

}

1 个答案:

答案 0 :(得分:0)

看看这个

windows form treeview with dynamic child

这里的答案将让您了解如何为员工实施treeView