Wednesday 25 March 2015

Sorting GridView Selected DropDownList Values in Asp.Net C#

Sorting GridView Selected DropDownList Values


Sorting GridView  Details When Select DropDownList Items Display in Asp.Net C#.

                              
                                  DEMO


HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

</head>
<body>
    <form id="form1" runat="server">
      <div>
           
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
      

         <table><tr><td> </td><td>   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

               <AlternatingRowStyle BackColor="#9999FF" />
                 <Columns>
                <asp:BoundField DataField="name" HeaderText="Name" />

              <asp:BoundField DataField="country" HeaderText="Country" />
               </Columns>

                 <HeaderStyle BackColor="#FF3300" />
                   <RowStyle BackColor="#FF99FF" /> 
          
            </asp:GridView>

             </td></tr></table>  
        </div>
    </form>
</body>
</html>

C# CODING

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;


public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            country();
            dropdown();
        }
    }
    protected void country()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();

        SqlCommand cmd = new SqlCommand("select country from country", con);
        SqlDataReader rd = cmd.ExecuteReader();
        DropDownList1.Items.Clear();
        DropDownList1.Items.Add("Select Country");
        while (rd.Read())
        {

            DropDownList1.Items.Add(rd["country"].ToString());
        }
   
    }
    protected void dropdown()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        if (DropDownList1.SelectedValue != "")
        {

            SqlCommand cmd = new SqlCommand("select * from country where country='" + DropDownList1.Text + "'", con);

            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
          
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
      
    }
  
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        dropdown();
    }
}






























0 comments:

Post a Comment