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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted May 6, 2012, 6:28 AM
<%@ 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 coolPosted May 6, 2012, 1:37 AM
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..