Monday, 2 March 2020

C# - ASP.NET - GridView - TemplateField - DropDownListItem


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Gridview_DDL.WebForm1" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                             OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField HeaderText="Name" DataField="ContactName" />
        <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>


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Gridview_DDL
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource =
                     GetData("SELECT ContactName, Country FROM Customers");
                GridView1.DataBind();
            }
        }

        private DataSet GetData(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 ddlCountries =
                                (e.Row.FindControl("ddlCountries") as DropDownList);
                ddlCountries.DataSource =
                                GetData("SELECT DISTINCT Country FROM Customers");
                ddlCountries.DataTextField = "Country";
                ddlCountries.DataValueField = "Country";
                ddlCountries.DataBind();

                // Add Default Item in the DropDownList
                ddlCountries.Items.Insert(0, new ListItem("Please select"));

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

No comments:

Post a Comment