select tree node to display gridview .
i have two tables with ID .
when i select a tree node to display a related data in a girdview
in a tree nodes are fetch in a table
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted May 6, 2012, 2:28 AM EDT
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Treenode_to_display_gridview._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Treenode_to_display_gridview
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
TreeNode PNode = new TreeNode();
string query;
int cnt;
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
trvw.Nodes.Clear();
query = "Select * from treeexample";
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand(query, con);
da = new SqlDataAdapter(com);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (cnt = 0; cnt <= dt.Rows.Count - 1; cnt++)
{
dr = dt.Rows[cnt];
switch (dr["id"].ToString())
{
case "1":
PNode = new TreeNode(dr["valuename"].ToString().Trim(), dr["valuename"].ToString().Trim());
this.trvw.Nodes.Add(PNode);
break;
case "2":
PNode.ChildNodes.Add(new TreeNode(dr["valuename"].ToString().Trim(), dr["valuename"].ToString().Trim()));
break;
}
PNode.ExpandAll();
}
}
con.Close();
}
protected void trvw_SelectedNodeChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand("select * from clrtbl where clr='" + trvw.SelectedNode.Text + "'", con);
da = new SqlDataAdapter(com);
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
}
}
Thanks
ClonecMr cool31 reputationPosted May 5, 2012, 9:37 PM EDT
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode theNode = this.treeView1.GetNodeAt(e.X, e.Y);
string yyy = this.treeView1, theNode.Tag.ToString();
//and you find the value from dataGrid
dataGridView1.SelectedDataKey.Values["yyy"].toString();
}
In My Code :
*I used one Dataset for that TreeView and that DataGridView
*I used Node.Tag to get a Value that have relationship.
*The Node tag is value from column name of DataRow to read.
hope this help..