Friday, 4 January 2013

Bind a GridView to a Tree View node selection in asp.net C#

Step 1) Create a Empty Asp.net website using C# 4.0/4.5
Step 2) Add a new Webpage, name it as default.aspx
Step 3)  Add TreeView and GridView to aspx page

<asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"></asp:TreeView>
                   </td>

            <td>
                <asp:GridView ID="GridView1" runat="server" EmptyDataText="no data found"></asp:GridView>

Step 4)  Get Data from the NorthWind Database.

 private void BuildDataSet()
        {
            SqlConnection conn = new SqlConnection(@"server=(local)\sqlexpress2012;uid=sa;pwd=lordsiva2030&;database=Northwind;");
            conn.Open();
            SqlCommand selCmd = new SqlCommand("select distinct ShipCOUNTRY from [Northwind].[dbo].[Orders]  for xml auto,root", conn);
           // SqlDataReader reader =selCmd.ExecuteReader(CommandBehavior.CloseConnection);
            XmlReader xmlReader=selCmd.ExecuteXmlReader();
            AddNodes(xmlReader);
            xmlReader.Close();
            SqlDataAdapter ad = new SqlDataAdapter("select * from [Northwind].[dbo].[Orders]", conn);
            ad.Fill(ds);
            Session["ds"] = ds;
        }


Getting XML data using ExecuteXmlReader
Getting orders using SQLDataAdapter.

Step 5)  Add Nodes to TreeView

ExecuteXmlReader  returned data will be added to TreeView

        void AddNodes(XmlReader re)
        {
            while (re.Read())
            {
                if (re.Depth !=0)
                {re.MoveToAttribute(0);
                    String country = re.Value;
                    TreeView1.Nodes.Add(new TreeNode { Text = country, Value = country });
                }
            }
        }
 


Step 6) Add Event Handler for Treeview node selection change
TreeView1_SelectedNodeChanged

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            Response.Write(TreeView1.SelectedValue);
            ds = (DataSet)Session["ds"];
            DataView dataview=ds.Tables[0].AsDataView(); 

           //Filter rows based on TreeView node selection
            dataview.RowFilter = "[ShipCountry]=\'" + TreeView1.SelectedValue+"\'";
            GridView1.DataSource = dataview;
            GridView1.DataBind();

        }


Step 7) Run the WebPage 


Bind a GridView to a Tree View node selection in asp.net C#
filter gridview based on treeview node selection using C# in asp.net
















Complete Source Code

 

TreeViewDemo2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeViewDemo2.aspx.cs" Inherits="WebApplication1.TreeViewDemo2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind a GridView to a Tree View node selection in asp.net C#</title>
    <style type="text/css">
        td
        {
            vertical-align:top;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Bind a GridView to a Tree View node selection in asp.net C#</h1>
       
                <table><tr><td>

            <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"></asp:TreeView>
                   </td>

            <td>
                <asp:GridView ID="GridView1" runat="server" EmptyDataText="no data found"></asp:GridView>
            </td>
               </tr></table>
       
    </div>
    </form>
</body>
</html>

 

TreeViewDemo2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
namespace WebApplication1
{
    public partial class TreeViewDemo2 : System.Web.UI.Page
    {
        DataSet ds = new DataSet("NorthWndOrders");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BuildDataSet();
            else
            {
            }
        }

        private void BuildDataSet()
        {
            SqlConnection conn = new SqlConnection(@"server=(local)\sqlexpress2012;uid=sa;pwd=pwd&;database=Northwind;");
            conn.Open();
            SqlCommand selCmd = new SqlCommand("select distinct ShipCOUNTRY from [Northwind].[dbo].[Orders]  for xml auto,root", conn);
           // SqlDataReader reader =selCmd.ExecuteReader(CommandBehavior.CloseConnection);
            XmlReader xmlReader=selCmd.ExecuteXmlReader();
            AddNodes(xmlReader);
            xmlReader.Close();
            SqlDataAdapter ad = new SqlDataAdapter("select * from [Northwind].[dbo].[Orders]", conn);
            ad.Fill(ds);
            Session["ds"] = ds;
        }

        void AddNodes(XmlReader re)
        {
            while (re.Read())
            {
                if (re.Depth !=0)
                {re.MoveToAttribute(0);
                    String country = re.Value;
                    TreeView1.Nodes.Add(new TreeNode { Text = country, Value = country });
                }
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            Response.Write(TreeView1.SelectedValue);
            ds = (DataSet)Session["ds"];
            DataView dataview=ds.Tables[0].AsDataView();
            dataview.RowFilter = "[ShipCountry]=\'" + TreeView1.SelectedValue+"\'";
            GridView1.DataSource = dataview;
            GridView1.DataBind();

        }
    }
}