Wednesday, 16 January 2013

filter gridview using dropdownlist Vb.NET

filter gridview using dropdownlistVb.NET

filter gridview using dropdownlist C#/VB.NET

DropDownList to Filter GridView

This example explains how to filter gridview using DropDownlist in

Step 1) Add GridView and DropDownList.

filter gridview using dropdownlist C# VB.NET

        <asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager>
            <asp:Label ID="Label4" runat="server"
                Text="<h1>Filter Gridview using DropDownList<h1>"
            <tr><td style="vertical-align:top;">
                 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server"


//Update Panel should be conditional because DropDownList exists outside of the

//UpdatePanel  .
//Only this UpdatePanel content will be Updated and UpdatePanels with UpdateMode='Always' also will be updated.

            </td><td style="vertical-align:top">
        <asp:UpdatePanel runat="server" UpdateMode="Conditional">
        <asp:GridView ID="GridView1" runat="server"
             ShowFooter="True" AutoGenerateColumns="false"
             EmptyDataText="No data found"

            <FooterStyle BackColor="YellowGreen" />
                <asp:TemplateField HeaderText="Shipped Date">
                        <asp:Label runat="server" Text='<%#Eval("ShippedDate","{0:D}") %>'>

                <asp:TemplateField HeaderText="OrderID">
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("OrderID") %>'>

                        <asp:Label ID="Label3" runat="server" Text="Total">


                <asp:TemplateField HeaderText="Subtotal">
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("Subtotal","{0:C}") %>'>

                <asp:TemplateField HeaderText="Year">
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("Year") %>'>


                <asp:AsyncPostBackTrigger ControlID="DropDownList1" />


Step 2)  Call SQL Stored Procedure to get the DATA.

  Private Sub Calling_Stored_Procedures_using_adonet(begingdate As DateTime, enddate As DateTime)
        Dim conn As New SqlConnection("Server=.\sqlexpress2012;" & vbCr & vbLf & "                                               database=northwind;trusted_connection=yes;")

        'SqlCommand sel = new SqlCommand(stroredProc);
        Dim sel As New SqlCommand()
        Dim sqlBegin As SqlParameter = sel.Parameters.Add("@Beginning_Date", SqlDbType.DateTime)
        sqlBegin.Direction = ParameterDirection.Input
        sqlBegin.Value = begingdate
        Dim sqlEnd As SqlParameter = sel.Parameters.Add("@Ending_Date", SqlDbType.DateTime)
        sqlEnd.Direction = ParameterDirection.Input
        sqlEnd.Value = enddate
        sel.CommandType = CommandType.StoredProcedure
        sel.CommandText = "[dbo].[Sales by Year]"
        sel.Connection = conn
        Dim ad As New SqlDataAdapter(sel)


    Catch ex As SqlException
    End Try
End Sub

Step 3)  First Bind to DropDownList  

Private Sub BindDropDownList()

    If Not IsPostBack Then
        'Using LINQ to DATATABLE , select only Distinct Values
        Dim query = ().Distinct()
        DropDownList1.DataSource = query
        DropDownList1.Items.Insert(0, "--Year--")
    End If

End Sub

Step 4)   Add DropDownList   Event Handler SelectedIndexChanged,

Make AutoPostBack=true

In this actually bind to GridView using Selected Value in DropDownList

       Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim year As [String] = DropDownList1.SelectedValue.ToString()

    Dim view As DataView = dt.DefaultView

    view.RowFilter = "Year='" + year + "'"

    GridView1.DataSource = view

End Sub

filter gridview using dropdownlist C#/VB.NET,Stored procedures ,

Tags:Asp:GridView,asp:DropDownList,ADO.NET Stored Procedures,ADO.NET DataTable,DropDownList SelectIndexChanged,FIlter GridView based on DropDownList value,Filtering GridView using DropDownList VB.NET,LINQ to DataTable Distinct,UPDATEPANEL CONDITIONAL,AJAX GRIDVIEW,AJAX DROPDOWNLIST, GridView Template Controls,GridView FooterTemplate

No comments:

Post a Comment