Friday, 20 June 2014

how to bind data to Dropdownlist inside gridview in asp.net






Description:-

In this example we explain that how to bind Dropdownlist inside gridview or how to populate dropdownlist inside gridview ItemTemplate in asp.net.

Here in this example we have one table called employee and have two field like empname and country. When page is load the data of employyes table can be bind in gridview with empname in label and country is bind on the dropdownlist inside the gridview.

Here we define RowDatabound event of the gridview so one by one row of the gridview is binded with empname and it’s selected country and user can also change the employees country in the dropdownlist.

extract or convert unzip files to zip folder extract unzip files to zip file in asp.net

bind Accordion panel with dynamic data bind ajax accordion panel with dynamic data


Binddrpdwningrdview.aspx:-

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to bind data to DropDownList in GridView in ASP.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
        <Columns>
            <asp:BoundField HeaderText="EmployeeName" DataField="cmpname" />
            <asp:TemplateField HeaderText="Country">
                <ItemTemplate>
                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible="false" />
                    <asp:DropDownList ID="ddlCountries" runat="server">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>


Binddrpdwningrdview.aspx.cs:-

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class binddrpdwningrdviewaspx : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = BindGridview("SELECT empname,Country FROM Employees");
            GridView1.DataBind();
        }
    }

    private DataSet BindGridview(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the DropDownList in the Row
            DropDownList drpcountry = (e.Row.FindControl("ddlCountries") as DropDownList);
            drpcountry.DataSource = BindGridview("SELECT DISTINCT Country FROM Employees");
            drpcountry.DataTextField = "Country";
            drpcountry.DataValueField = "Country";
            drpcountry.DataBind();

            //Add Default Item in the DropDownList
            drpcountry.Items.Insert(0, new ListItem("<-- select -->"));

            // Select the Country of Customer in DropDownList
            string country = (e.Row.FindControl("lblCountry") as Label).Text;
            drpcountry.Items.FindByValue(country).Selected = true;
        }
    }

}


0 comments:

Post a Comment