Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DBObj class, which holds SQL Server and MS Access database information, such as database name, server, path and id.

I have a TreeView control, whereby the text label of each TreeView is the name of the database.

I would like to made one db call, which populates a List<dbobj>, but I don't want to have to peek into the List to get the database name which will populate the TreeView.

I am not super familiar with lists, so I'm unsure both what is available and how to do it.

Perhaps something like List<DBName,DBObj> ?

Any suggestions would be great.

What I have tried:

I have tried this the old fashioned way, by creating a scaler list of strings, which held the database paths and then upon each retrieval of a database path from the list, I do a db lookup to look for the rest for the rest of the info.

My THEORY is that this is going to become resource intensive, however, if you think that to be in error, please let me know.
Posted
Updated 10-Dec-17 20:59pm
Comments
BillWoodruff 11-Dec-17 10:32am    
"the text label of each TreeView" I think you mean each TreeNode.

if you're using a TreeView, I assume you are dealing with objects in a hierarchy; but, your post seems to describe a just a "flat" collection. Please clarify.

1 solution

Be object-oriented:

public class MyClass
{
	public List<MyClass> MyChildren { get; set; }
	public MyClass MyParent { get; set; }
}


Fill this virtual tree from DB , than simple recursive function can show it in TreeView:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTreeView
{

	public partial class Form1 : Form
	{
		MyClass FirstElement;
		public Form1()
		{
			InitializeComponent();
			FirstElement = new MyClass() { Name = "I am first" };


			FirstElement.MyChildren.Add(new MyClass() { Name = "SecondLevel" });
			MyClass oneMoreElement = new MyClass() { Name = "Something" };

			FirstElement.MyChildren[0].MyChildren.Add(oneMoreElement);
			//now we have a tree. this is not simple array.

		}

		private void button1_Click(object sender, EventArgs e)
		{
			GetAllFrom(FirstElement , this.treeView1.Nodes);
		}

		

		private void GetAllFrom(MyClass LevelElement, TreeNodeCollection treeNodeCollection)
		{
			TreeNode anyLevel = new TreeNode();
			anyLevel.Text = LevelElement.Name;
			treeNodeCollection.Add(anyLevel);
			foreach (MyClass anyChild in LevelElement.MyChildren)
			{
				GetAllFrom(anyChild , anyLevel.Nodes);
			}

		}		
	}


	public class MyClass
	{
		List<MyClass> myChildren;
		public List<MyClass> MyChildren
		{
			get
			{
				if (myChildren == null)
				{
					myChildren = new List<MyClass>();
				}
				return myChildren;
			}
			set
			{
				myChildren = value;
			}
		}

		//public MyClass MyParent { get; set; }
		public string Name { get; set; }

		
	}
}


On Form1 only TreeView and button.
 
Share this answer
 
v2
Comments
[no name] 21-Dec-17 1:29am    
I saw this when you posted it, but I'm a little confused by this. I guess I'd need to see an example. At any rate, what I ended up doing was creating a class from my database tables and stored the populated object in the TreeView's Tag object. Then, I created a List<myobj> to hold the database information that corresponds to the selected TreeView node.
________________ 21-Dec-17 3:41am    
I did small example, have a look. Parent is not in use, how to fill the chain - there is no universal solution - all depends of data source.
Wy Winform? WPF mach more powerful, and it really have something almost you want - hierarchic data template. but it hardly can be implemented in Winform...
[no name] 22-Dec-17 1:18am    
Ok, I see where you're going with that. I chose WinForms because I'm a database developer and WinForms was easier to understand. I couldn't get past the XML interface. It doesn't make sense to me. I'm sure I will understand once it's explained to me, but I'm learning on my own, so this is my only way to speak to anyone about it. LOL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900