Follow us

Header Ads

Bind Dropdown List with GridView

BindDropdownWithGridview.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1BindDropdownWithGridview.aspx.cs" Inherits="GridviewWithHelpDropdown" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>Bind Dropdown List With GridView</h1>
            <br />
            <br />
            <asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="183px" >
            </asp:DropDownList>
            &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

BindDropdownWithGridview.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.SqlClient;
using System.Configuration;
using System.Data;
using System.Drawing;

public partial class GridviewWithHelpDropdown : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDownList();
        }

    }
    void BindDropDownList()
    {
        SqlConnection con = new SqlConnection(cs);
        string query = "select * from employee";
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable data = new DataTable();
        sda.Fill(data);
        DropDownList1.DataSource = data;
        //Nagar aayaga name par database me id jayga.
        DropDownList1.DataTextField = "name";
        DropDownList1.DataValueField = "id";
        DropDownList1.DataBind();
        //First item show hoga
        ListItem SelectItem = new ListItem("Select Employee", "-1");
        SelectItem.Selected = true;
        DropDownList1.Items.Insert(0, SelectItem);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "-1")
        {
            Response.Write("Please select an employee");
        }
        else
        {
            SqlConnection con = new SqlConnection(cs);
            string query = "select * from employee where name=@name";
            SqlDataAdapter sda = new SqlDataAdapter(query, con);
            sda.SelectCommand.Parameters.AddWithValue("@name",DropDownList1.SelectedItem.Text);
            DataTable data = new DataTable();
            sda.Fill(data);
            GridView1.DataSource = data;
            GridView1.DataBind();
            Label1.Text = "Rows Found";
            Label1.ForeColor = Color.Green;
            Label1.Visible = true;
        }
    }
}

Post a Comment

0 Comments