' right now i am working on treeview i one problem with this i have how can i view data in datagridview by selecting node in treeview i have connected treeview with database when i select one node the data of the node should display in datagridveiw ex:- if i select empolyeename the details of empolyee should display in datagridview . please could any one heolp in this.
thanks
Ashok KumarPosted Jul 2, 2012, 2:12 AM
Satyapriya NayakPosted Jul 2, 2012, 1:54 AM
Try this...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Treeview_Node__Related_data
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.Nodes.Add("Please select any table");
treeView1.Nodes.Add("student");
treeView1.Nodes.Add("employee");
treeView1.Nodes.Add("product");
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
if (treeView1.SelectedNode.Text == "Please select any table")
{
}
else if (treeView1.SelectedNode.Text == "student")
{
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "student");
dataGridView1.DataMember = "student";
dataGridView1.DataSource = ds;
con.Close();
dataGridView1.Visible = true;
dataGridView2.Visible = false;
dataGridView3.Visible = false;
}
else if (treeView1.SelectedNode.Text == "employee")
{
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView2.DataMember = "employee";
dataGridView2.DataSource = ds;
con.Close();
dataGridView1.Visible = false;
dataGridView2.Visible = true;
dataGridView3.Visible = false;
}
else if (treeView1.SelectedNode.Text == "product")
{
con.Open();
str = "select * from product";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "product");
dataGridView3.DataMember = "product";
dataGridView3.DataSource = ds;
con.Close();
dataGridView1.Visible = false;
dataGridView2.Visible = false;
dataGridView3.Visible = true;
}
}
}
}
Thanks
If this post helps you mark it as answer