Friday, 4 January 2013

Bind a GridView to a Tree View node selection in asp.net VB.NET

Step 1) Create a Empty Asp.net website using VB.NET 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  Sub BuildDataSet()
            Dim conn As SqlConnection =  New SqlConnection("server=(local)\sqlexpress2012;uid=sa;pwd=pwd&;database=Northwind;")
            conn.Open()
            Dim selCmd As SqlCommand =  New SqlCommand("select distinct ShipCOUNTRY from [Northwind].[dbo].[Orders]  for xml auto,root",conn)
           ' SqlDataReader reader =selCmd.ExecuteReader(CommandBehavior.CloseConnection);
            Dim xmlReader As XmlReader = selCmd.ExecuteXmlReader()
            AddNodes(xmlReader)
            xmlReader.Close()
            Dim ad As SqlDataAdapter =  New SqlDataAdapter("select * from [Northwind].[dbo].[Orders]",conn)
            ad.Fill(ds)
            Session("ds") = ds
        End Sub

Getting XML data using ExecuteXmlReader
Getting orders using SQLDataAdapter.

Step 5)  Add Nodes to TreeView

ExecuteXmlReader  returned data will be added to TreeView



        Private  Sub AddNodes(ByVal re As XmlReader)
            While re.Read()
                If re.Depth <>0 Then
                {re.MoveToAttribute(0)
                End If
                    Dim counTry As String =  re.Value
                    TreeView1.Nodes.Add(New TreeNode
                    {
                         Text = counTry, Value = counTry
                    }
)
                }
            End While
        End Sub

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


        Protected  Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs)
            Response.Write(TreeView1.SelectedValue)
            ds = CType(Session("ds"), DataSet)
            Dim dataview As DataView = ds.Tables(0).AsDataView()
            dataview.RowFilter = "[ShipCountry]=\'" + TreeView1.SelectedValue+"\'"
            GridView1.DataSource = dataview
            GridView1.DataBind()

        End Sub
    End Class
End Namespace


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 VB.NET</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 VB.NET</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.vb

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Xml.XPath
Namespace WebApplication1
    Public partial Class TreeViewDemo2
     Inherits System.Web.UI.Page
        Dim ds As DataSet =  New DataSet("NorthWndOrders")
        Protected  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                BuildDataSet()
            Else
            End If
        End Sub

        Private  Sub BuildDataSet()
            Dim conn As SqlConnection =  New SqlConnection("server=(local)\sqlexpress2012;uid=sa;pwd=lordsiva2030&;database=Northwind;")
            conn.Open()
            Dim selCmd As SqlCommand =  New SqlCommand("select distinct ShipCOUNTRY from [Northwind].[dbo].[Orders]  for xml auto,root",conn)
           ' SqlDataReader reader =selCmd.ExecuteReader(CommandBehavior.CloseConnection);
            Dim xmlReader As XmlReader = selCmd.ExecuteXmlReader()
            AddNodes(xmlReader)
            xmlReader.Close()
            Dim ad As SqlDataAdapter =  New SqlDataAdapter("select * from [Northwind].[dbo].[Orders]",conn)
            ad.Fill(ds)
            Session("ds") = ds
        End Sub

        Private  Sub AddNodes(ByVal re As XmlReader)
            While re.Read()
                If re.Depth <>0 Then
                {re.MoveToAttribute(0)
                End If
                    Dim counTry As String =  re.Value
                    TreeView1.Nodes.Add(New TreeNode
                    {
                         Text = counTry, Value = counTry
                    }
)
                }
            End While
        End Sub

        Protected  Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs)
            Response.Write(TreeView1.SelectedValue)
            ds = CType(Session("ds"), DataSet)
            Dim dataview As DataView = ds.Tables(0).AsDataView()
            dataview.RowFilter = "[ShipCountry]=\'" + TreeView1.SelectedValue+"\'"
            GridView1.DataSource = dataview
            GridView1.DataBind()

        End Sub
    End Class
End Namespace