Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulate treeview from database (two classes)
    text
    copied!<p>I found many threads with this theme but all are about situation where populating treeview and communication with database is in one class... I wanted to separate these and i have one class with my form where i want to populate treeview with data that i get from other class. That other class's "job" is only to communicate with database and pass data where it is needed... Here is my class with form:</p> <pre><code>public Form1() { InitializeComponent(); } // on Form load private void Form1_Load(object sender, EventArgs e) { List&lt;String&gt; lista = DB.DatabaseBroker.GetSports(); PopulateTreeView(lista); } private void PopulateTreeView(List&lt;string&gt; lista) { treeViewSports.Nodes.Clear(); foreach (string s in lista) { treeViewSports.Nodes.Add(s); } } </code></pre> <p>...and here is class where i get data from:</p> <pre><code>static SqlConnection conn = new SqlConnection("Data Source=MLAD3N-PC\\SQLEXPRESS;Initial Catalog=DBSports;Integrated Security=True"); public static List&lt;String&gt; GetSports() { DataTable dataTable = new DataTable(); List&lt;String&gt; sports = new List&lt;String&gt;(); SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM TableSport", conn); SDA.Fill(dataTable); if (dataTable.Rows.Count &gt; 0) { foreach(DataRow drow in dataTable.Rows) { sports.Add(drow[1].ToString()); } } return sports; } </code></pre> <p>with this i get treeview with sport that looks like this: <img src="https://i.stack.imgur.com/384eg.jpg" alt="enter image description here"></p> <p>The thing is, i want to add leagues to this treeview so under football i have child nodes with football's leagues. In my database, leagues table contain foreign keys from sport table, so if my sport football have sportID=1, all leagues in LeagueTable that corresponds with football have foreign key sportID=1.</p> <p>My question is, how can i get needed leagues from database and place them under football node?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload