2016-07-24 4 views
-1

В моем приложении мне нужна структура древовидной структуры с дочерними элементами, appilcation должен быть таким, чтобы я мог иметь несколько дочерних узлов с большим количеством узлов. Когда пользователь нажимает на те узлы, должны получить расширения, как я могу это сделатькак мы можем получить структуру treeview с nultiple childnodes

+0

У вас есть уязвимость в SQL-инъекции. – SLaks

+0

Вы видели свойство 'Columns'? – SLaks

ответ

0

Если я понимаю, ваш средний вы можете добавить дополнительный узел в качестве заголовка, например:

private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode) 
    { 


     foreach (DataRow row in dtParent.Rows) 
     { 
      string Value = string.Empty; 
      string header = string.Empty; 
      // this is just extra node for header 
      if (dtParent.Columns.Count > 2) 
      { 
       header += "Name" + " " + " "; 
       header += "VehicleType" + " " + " "; 
       header += "Capacity" + " " + " "; 
      } 
      else 
      { 
       header += "Name" + " " + " "; 
      } 
      TreeNode headerNode = new TreeNode 
      { 
       Text = header.ToString(), 
       // Any value that you want and has no confelict with other 
       Value = row["Id"].ToString() 

      }; 


      if (dtParent.Columns.Count > 2) 
      { 
       Value += row["Name"].ToString() + " " + " "; 
       Value += row["VehicleType"].ToString() + " " + " "; 
       Value += row["Capacity"].ToString() + " " + " "; 
      } 
      else 
      { 
       Value += row["Name"].ToString() + " " + " "; 
      } 

      TreeNode child = new TreeNode 
      { 
       Text = Value.ToString(), 
       Value = row["Id"].ToString() 

      }; 



      if (parentId == 0) 
      { 
       TreeView1.Nodes.Add(child); 
       DataTable dtChild = this.GetData("SELECT Id, Name,VehicleType,Capacity FROM Bikes WHERE VehicleTypeId = " + child.Value); 
       PopulateTreeView(dtChild, int.Parse(child.Value), child); 
      } 
      else 
      { 
       treeNode.ChildNodes.Add(child); 
      } 
     } 
    } 

или если среднее значение нахождения каждого имени столбца можно использовать следующим образом:

private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode) 
    { 


     foreach (DataRow row in dtParent.Rows) 
     { 
      string Value = string.Empty; 
      string header = string.Empty; 
      // Get all columns name 
      for (var i=0;i<(dtParent.Columns.Count)-1;i++) 
      { 
       header += dtParent.Columns[i].ColumnName + " "; 
      } 

      TreeNode headerNode = new TreeNode 
      { 
       Text = header.ToString(), 
       // Any value that you want and has no confelict with other 
       Value = row["Id"].ToString() 

      }; 


      if (dtParent.Columns.Count > 2) 
      { 
       Value += row["Name"].ToString() + " " + " "; 
       Value += row["VehicleType"].ToString() + " " + " "; 
       Value += row["Capacity"].ToString() + " " + " "; 
      } 
      else 
      { 
       Value += row["Name"].ToString() + " " + " "; 
      } 

      TreeNode child = new TreeNode 
      { 
       Text = Value.ToString(), 
       Value = row["Id"].ToString() 

      }; 



      if (parentId == 0) 
      { 
       TreeView1.Nodes.Add(child); 
       DataTable dtChild = this.GetData("SELECT Id, Name,VehicleType,Capacity FROM Bikes WHERE VehicleTypeId = " + child.Value); 
       PopulateTreeView(dtChild, int.Parse(child.Value), child); 
      } 
      else 
      { 
       treeNode.ChildNodes.Add(child); 
      } 
     } 
    } 
Смежные вопросы