简体   繁体   中英

Binding data to treeView c#

I wish my program worked like this: 1.It takes data form MS SQL 2.Makes tree based on this data(data rows) 3.I have few textBoxex and I want to fill them with data based on clicked node. I`ve made class for SQL connection:

 class Baza
{
    private SqlConnection connection;
    string dbdir = "Data Source=CS24\\SQLEXPRESS;user id=sa;password=alamakota;database=SHARP;connection timeout=3";
    public Baza()
    {
      connection = new SqlConnection();
      connection.ConnectionString = dbdir;
      connection.Open();
    }
    public DataTable get_data(string q) 
    {
        DataTable dt = new DataTable(); 
        SqlDataReader dr ; 
        SqlCommand sqlc = new SqlCommand(q);
        sqlc.Connection = this.connection;
        dr = sqlc.ExecuteReader();
        dt.Load(dr);
        return dt;
    }
}

And treeView creator:

  private void Form1_Load(object sender, EventArgs e)
    {
        Baza baza = new Baza();
        DataTable dt = new DataTable();
        dt = baza.get_data("Select * from Users order by Id asc");
        foreach (DataRow dr in dt.Rows) 
        {
            TreeNode node = new TreeNode(dr["Name"].ToString() + " " + dr["Surname"].ToString());
            treeView1.Nodes.Add(node);
        }
    }

I thought that node should have ID which I can use to make data filling but I dont really know how to do it. Can you help me with that?

I had the same trouble and i did this :

(I had hierarchical data in columns ParentID, IsGroup, OrderInGroup)

  • Create a class for the TreeViewItems

      public class xTreeViewItem : INotifyPropertyChanged { public int id { get; set; } public string Header { get; set; } public int parent; public List<xTreeViewItem > children { get; set; } public bool isGroup = false; public bool ParentResolved = false; public float order {get;set;} public void NotifyPropertyChanged(string info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public event PropertyChangedEventHandler PropertyChanged; } 
  • Now make a list with the SQL Data

     private void LoadTree() { ObservableCollection<xTreeViewItem> treeSource = new ObservableCollection<xTreeViewItem>(); List<xTreeViewItem> Buffer = new List<xTreeViewItem>(); foreach (DataRow row in table.Rows) { xTreeViewItem itm = new TreeViewItem(); // Load the required heirarchial data Buffer.Add(itm); } for (int i = Buffer.Count - 1; i >= 0; i--) { if (Buffer[i].parentID != 0) { if (Buffer[i].ParentResolved) Buffer.RemoveAt(i); else { Buffer[i].ParentResolved = true; while (Buffer.Any(c => (c.parentID == Buffer[i].menuID && !c.ParentResolved))) { Buffer[i].children.Add(FindChildren(Buffer[i])); } xTreeViewItem parent = Buffer.First(c => c.menuID == Buffer[i].parentID); parent.children.Add(Buffer[i]); Buffer[i].parent = parent; Buffer.RemoveAt(i); } } } // remaining items acts as mainItems, so add it to the treeSource foreach (xTreeViewItem x in Buffer) { treeSource.Add(x); } treeView.ItemsSource = treeSource; } xTreeViewItem FindChildren(xMenuItem x) { List<xTreeViewItem > subBuffer = Buffer.Where(c => (c.parentID == x.menuID && !c.ParentResolved)).ToList(); // if there is, get all of them to a list subBuffer = subBuffer.OrderBy(c => c.order).ToList(); // << THIS IS CRAZY, BUT FIXES :P // order all of them by their 'order' property int indx = Buffer.IndexOf(subBuffer[0]); // get the first item after ordering Buffer[indx].ParentResolved = true; Buffer[indx].parent = x; // changing parentresolved property to prevent infinte loops while (Buffer.Any(c => (c.parentID == Buffer[indx].menuID && !c.ParentResolved))) // again checking if the current child got any other child. { Buffer[indx].children.Add(FindChildren(Buffer[indx])); // adding all the childs } return Buffer[indx]; // return the child } 

In XAML : put this as the TreeView's ItemTemplate :)

    <TreeView.ItemTemplate>
          <HierarchicalDataTemplate ItemsSource="{Binding children}">
                  <ContentPresenter Content="{Binding Header}" Margin="2,0"/>
          </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>

This did the work for me :P and to get the selected item, just use :

xTreeViewItem selectedItem = (xTreeViewItem)treeView.SelectedItem;

make your view update with this 'selectedItem'; .. :)

Hope it helped :)

** Sometimes the items may appear reversed. Simply give the List.Reverse(); in the point where it is so, i havent run the code myself, anyway this should work for all types, like loading MenuItems from Database and desplaying (useful if you have multiple languages) :)

** Im just 18 yrs old :P havent got a degree in CS, this is just a code that i came up with :) yea there might be flaws but it works for me :P

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM