How to populate a treeView from two or more tables in C# console Visual Studio 2012 -


my question is: how populate treeview multiple tables in visual studio 2012 , use treeview_afterselect_event datagridview changes based on selection in each node in treeview?

i know how insert 1 table @ time, don't know how going deploy following query in c# console: have 2 tables in database named bom$ , part$. query running in sql server.

select bom$.parent_name, bom$.component_name bom$ full outer join part$ on bom$.parent_name=part$.name order bom$.parent_name; 

below following code c# console visual studio 2012. let me know after_select_event well. i've finished learning diploma of information systems management i'm new; therefore appreciated.

namespace qbuild_test2 {     public partial class form1 : form     {         sqlconnection conn = new sqlconnection("data source=cominside-pc;initial catalog=qbuild_software;user id=sa;password=aptech");          public form1()         {             initializecomponent();         }          private void form1_shown(object sender, eventargs e)         {             try             {                 conn.open();             }             catch (sqlexception ex)             {                 messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error);                 application.exitthread();             }         }          private void populate_btn_click(object sender, eventargs e)         {             //on button click treeview control , datagrid view both populate @ same time directly database             sqlcommand cmd = new sqlcommand();             cmd.connection = conn;             cmd.commandtext = @"select parent_name,component_name,part_number,title,quantity,[type],item,material bom$ full outer join part$ on bom$.parent_name=part$.name order bom$.parent_name;";             datatable dt = new datatable();             sqldataadapter adapter = new sqldataadapter(cmd);             adapter.fill(dt);             datagridview.datasource = dt;              treeview.nodes.clear();             sqlcommand cmd2 = new sqlcommand(@"select * bom$ order parent_name asc");             cmd2.connection = conn;              // i'm able populate 1 table in treeview not both tables             try             {             sqldataadapter sda = new sqldataadapter(cmd2);             datatable dt2 = new datatable();             sda.fill(dt2);                foreach (datarow dr in dt2.rows)                   {                    treenode node = new treenode(dr["parent_name"].tostring());                    node.nodes.add(dr["quantity"].tostring());                    node.nodes.add(dr["component_name"].tostring());                    treeview.nodes.add(node);                   }                conn.close();              }             catch (exception ex)             {                 messagebox.show(ex.message, application.productname, messageboxbuttons.ok, messageboxicon.error);             }                 //this disable populate button once data populated                 populate_btn.enabled = false;          } //this code populate result selecting node based on selection failed.         private void treeview_afterselect(object sender, treevieweventargs e)         {             if(e.node.text != null)             {                 sqlcommand cmd = new sqlcommand();                 cmd.connection = conn;                 cmd.commandtext = @"select * bom$ order parent_name asc;";                 datatable dt = new datatable();                 sqldataadapter adapter = new sqldataadapter(cmd);                 adapter.fill(dt);                 datagridview.datasource = dt.defaultview;             }          }      } }  

looks looking this

http://www.c-sharpcorner.com/uploadfile/deepak.sharma00/how-to-bind-multiple-sql-server-tables-with-a-treeview-in-a/


Comments