Good day! Faced such a problem:

There is a code that receives data from a WCF service:

BasicHttpBinding bd = new BasicHttpBinding(); CashService.RemoteCashContractClient client = new CashService.RemoteCashContractClient(bd, new EndpointAddress("http://.......")); CashService.CatalogRecord[] myStocks = client.GetStockList(); 

But I can not understand how to display this data in the TreeView .

Please tell me how to do it correctly? Thank you in advance.

  • WinForms or Wpf? TreeView native or DevExpress for example? If the data acquisition code does not cause problems, adding it to the question makes no sense, it is better to add the code of how you tried to add data to the TreeView, and what did not work out - rdorn
  • @rdorn, I need to implement in a simple Windows Form, not in DevExpress, and preferably on .NET 4.0. There is really no example of creating my TreeView, since I don’t know how to implement it correctly. - Nikita Dyomin

1 answer 1

Standard TreeView in WinForms does not support data binding, therefore it must be filled in manually

 private void btnPopulate_Click(object sender, EventArgs e) { //Коллекция элементов которые будем добавлять в TreeView //тут может быть вызов сервиса например List<CatalogRecord> catalogRecords = new List<CatalogRecord>() { new CatalogRecord { Name = "Яблоко" }, new CatalogRecord { Name = "Груша" }, new CatalogRecord { Name = "Помидор" } }; //Так как стандартный TreeView не поддерживает привязку данных //то проходим по сформированной ранее коллекции и заполянем TreeView в ручную foreach(var record in catalogRecords) { tvTest.Nodes.Add(new TreeNode {Text = record.Name }); } } 
  • To imitate a binding, you can store a link to the "bound" element in the Tag property of the node, not that it is very convenient, but at least you can know someone clicked for example. - rdorn