Reputation: 1
My question is: how to populate a treeView from multiple tables in Visual Studio 2012 and than use treeView_afterSelect_event for DataGridView changes based on my selection in each node in treeView?
I know how to insert one table at a time, but I don't know how am I going to deploy the following query in C# console: I have two tables in my database named as bom$ and part$. Query is running successfully in SQL Server.
SELECT bom$.PARENT_NAME, bom$.COMPONENT_NAME
FROM bom$
FULL OUTER JOIN part$
ON bom$.PARENT_NAME=part$.NAME
ORDER BY bom$.PARENT_NAME;
Below is the following code from C# console visual studio 2012. Also let me know the after_select_event as well. I've just finished learning diploma of Information Systems Management so I'm new; therefore any help will be greatly 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 and datagrid view both will populate at the same time directly from the database
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = @"select PARENT_NAME,COMPONENT_NAME,PART_NUMBER,TITLE,QUANTITY,[TYPE],ITEM,Material FROM bom$ FULL OUTER JOIN part$ ON bom$.PARENT_NAME=part$.NAME ORDER BY 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 * from bom$ order by PARENT_NAME asc");
cmd2.Connection = conn;
// I'm able to populate one 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 will disable the populate button once the data populated
populate_btn.Enabled = false;
}
//this code is to populate result by selecting node based on the selection but failed.
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if(e.Node.Text != null)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = @"select * from bom$ order by PARENT_NAME ASC;";
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
dataGridView.DataSource = dt.DefaultView;
}
}
}
}
Upvotes: 0
Views: 2234