2013-04-22 7 views
1

DataTable에 DataTable을 바인딩하고 싶습니다. Code.Its에 따라 작성한 코드는 DataTable의 모든 데이터를 표시하지만 루트 노드는 표시하지 않습니다.TreeView에서 루트 노드를 동적으로 추가하는 방법

List<DocumentData> lstData = GetSPDocuments(); 
    gvDocuments.DataSource = lstData; 
    gvDocuments.DataBind(); 

    DataTable dt = ConvertToDataTable(lstData); 

    TreeNode node1 = new TreeNode("Root"); 


    foreach (DataRow r in dt.Rows) 
    { 
     int nodeLvl = int.Parse(r["ID"].ToString()); 
     string nodeParent = "Folders"; 
     string nodeName = r["Title"].ToString(); 


     TreeNode tNode = new TreeNode(nodeName); 

     ht.Add(nodeLvl.ToString() + nodeName, tNode); 

     if (tvDocs.Nodes.Count == 0) 
      tvDocs.Nodes.Add(tNode); 
     else 
     { 
      nodeLvl--; 
      tvDocs.Nodes.Add(tNode);    
     } 
    } 

여기에 정적 루트 노드를 추가하는 방법 ??? 도와주세요!

답변

4

시도해보십시오. 도움이 될 수 있습니다. 이것에 대한

protected void Page_Load(object sender, EventArgs e) 
    { 
     conStr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
     conn = new OleDbConnection(conStr); 
     BindTreeViewControl(); 
    } 

    private void BindTreeViewControl() 
    { 
     try 
     { 
      DataSet ds = GetDataSet("Select ProductId,ProductName,ParentId from ProductTable"); 
      DataRow[] Rows = ds.Tables[0].Select("ParentId = 0"); 

      for (int i = 0; i < Rows.Length; i++) 
      { 
       TreeNode root = new TreeNode(Rows[i]["ProductName"].ToString(), Rows[i]["ProductId"].ToString()); 
       root.SelectAction = TreeNodeSelectAction.Expand; 
       CreateNode(root, ds.Tables[0]); 
       treeviwExample.Nodes.Add(root); 
      } 
     } 
     catch (Exception Ex) { throw Ex; } 
    } 

    public void CreateNode(TreeNode node, DataTable Dt) 
    { 
     DataRow[] Rows = Dt.Select("ParentId =" + node.Value); 
     if (Rows.Length == 0) { return; } 
     for (int i = 0; i < Rows.Length; i++) 
     { 
      TreeNode Childnode = new TreeNode(Rows[i]["ProductName"].ToString(), Rows[i]["ProductId"].ToString()); 
      Childnode.SelectAction = TreeNodeSelectAction.Expand; 
      node.ChildNodes.Add(Childnode); 
      CreateNode(Childnode, Dt); 
     } 
    } 
    private DataSet GetDataSet(string Query) 
    { 
     DataSet Ds = new DataSet(); 
     try 
     { 
      OleDbDataAdapter da = new OleDbDataAdapter(Query, conn); 
      da.Fill(Ds); 
     } 
     catch (Exception dex) { } 
     return Ds; 
    } 

및 데이터베이스 구조는

enter image description here

+0

예제에서 나는 루트 노드를 Database.Can에 저장할 수 있습니다. 코드에서 추가 할 수 있습니다. 정적 루트 노드로 ??? – Gayatri

+0

이것을 TreeNode root = new TreeNode ("당신의 루트")로하고, 제 경우와 같이 treeviwExample.Nodes.Add (당신의 rootnode)를하고 있습니다; 하지만 더 나은 데이터베이스에서 parentid = 0을주고 ds.Tables [0] .Select ("ParentId = 0");로 루트 노드를 선택합니다. 나는 이것이 더 나은 접근 방법이라고 생각한다. – Mogli

0
// Suppress repainting the TreeView until all the objects have been created. 
treeView1.BeginUpdate(); 

// Clear the TreeView each time the method is called. 
treeView1.Nodes.Clear(); 

// create root node 
TreeNode root = new TreeNode("Root"); 

// loop and add all child nodes to root node 
foreach (DataRow r in dt.Rows) 
{ 
    // create child node 
    // add to root node 
    root.Nodes.Add(child); 
} 

// add root node to tree view 
treeView1.Nodes.Add(root); 

// Begin repainting the TreeView. 
treeView1.EndUpdate(); 
0

혹시이 대답나요입니까? 너 거의 다 왔어.

TreeView 컨트롤의 이름은 무엇입니까?

private TreeView treeView1; 

private void TreeView_DataBind() { 

    treeView1.Nodes.Clear(); 

    List<DocumentData> lstData = GetSPDocuments(); 
    gvDocuments.DataSource = lstData; 
    gvDocuments.DataBind(); 

    DataTable dt = ConvertToDataTable(lstData); 

    TreeNode node1 = new TreeNode("Root"); 

    treeView1.Nodes.Add(node1); // this is the step you missed 

    foreach (DataRow r in dt.Rows) 
    { 
    int nodeLvl = int.Parse(r["ID"].ToString()); 
    string nodeParent = "Folders"; 
    string nodeName = r["Title"].ToString(); 

    TreeNode tNode = new TreeNode(nodeName); 

    ht.Add(nodeLvl.ToString() + nodeName, tNode); 

    if (tvDocs.Nodes.Count == 0) 
     tvDocs.Nodes.Add(tNode); 
    else 
    { 
     nodeLvl--; 
     tvDocs.Nodes.Add(tNode);    
    } 
    } 

    node1.Expand(); 

} 

쉬워요 : 당신이 말한 적이 있기 때문에, 나는 아래의 것을 포함하도록 코드를 treeView1를 사용하고, 수정하고!

관련 문제