Please, help me to solve this:
I have a DropDownList, its items are country names. I would like to add the selected item of the DropDownList to a GridView after clicking a Button.
The GridView is composed of two columns: a BoundField Country and a DropDownList TemplateField City to show cities belongs to the selected country.
My Issue is when inserting a new row, the DropDownList TemplateField in all rows is updated to values of the DropDownList in last inserted row.
Database Schema:
CREATE TABLE [dbo].[tlkCountry](
[CountryID] [int] IDENTITY(1,1) NOT NULL,
[CountryName] [nvarchar](50) NULL
)
CREATE TABLE [dbo].[tlkCity](
[CityID] [int] IDENTITY(1,1) NOT NULL,
[CountryID] [int] NULL,
[CityName] [nvarchar](50) NULL
)
HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DropDownListInGridView.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">
<div>
<asp:Label runat="server">Country</asp:Label>
<asp:DropDownList runat="server" ID="ddlCounty" Width = "200px"
AutoPostBack="True">
</asp:DropDownList>
<asp:Button ID="btnAppend" runat="server" Text="Append" Width = "100px"
onclick="btnAppend_Click" />
</div>
<br />
<div>
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False"
Width = "300px" onrowdatabound="gvDetails_RowDataBound" >
<Columns>
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:DropDownList ID="ddlCity" runat="server" Width = "98%">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code Behind C#:
namespace DropDownListInGridView
{
public partial class WebForm1 : System.Web.UI.Page
{
static string Con = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection sqlCon = new SqlConnection(Con);
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
static DataTable dtGrid = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cmd = new SqlCommand();
cmd.Connection = sqlCon;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM tlkCountry";
try
{
dt = new DataTable();
sqlCon.Open();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
sqlCon.Close();
ddlCounty.DataSource = dt;
ddlCounty.DataTextField = "CountryName";
ddlCounty.DataValueField = "CountryID";
ddlCounty.DataBind();
dtGrid.Columns.Add("CountryName");
}
catch (Exception ex)
{
}
}
}
protected void btnAppend_Click(object sender, EventArgs e)
{
int i;
dtGrid.Rows.Add();
i = dtGrid.Rows.Count - 1;
dtGrid.Rows[i]["CountryName"] = ddlCounty.SelectedItem.Text;
gvDetails.DataSource = dtGrid;
gvDetails.DataBind();
}
protected void GetCitiesList(DropDownList ddl)
{
cmd = new SqlCommand();
cmd.Connection = sqlCon;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT CityID, CityName FROM tlkCity WHERE CountryID = " +ddlCounty.SelectedValue;
try
{
dt = new DataTable();
sqlCon.Open();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
sqlCon.Close();
string s = dt.Rows.Count.ToString();
ddl.DataSource = dt;
ddl.DataTextField = "CityName";
ddl.DataValueField = "CityID";
ddl.DataBind();
}
catch (Exception ex)
{
}
}
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList[] _ddl= new DropDownList[10];
_ddl[e.Row.RowIndex]= e.Row.FindControl("ddlCity") as DropDownList;
GetCitiesList(_ddl[e.Row.RowIndex]);
}
}
}
}