indradeo kumar

indradeo kumar

  • 1.2k
  • 546
  • 7.5k

how to add dropdown list with gridview and update when sele

Jul 26 2019 5:48 AM
Dear Team,
kindly find my code details and help to complate same still i unable to complate database structur.
.aspx==
  1. <%@ Page Language="C#" AutoEventWireup="true" Codefile="DeptCotractDetai;s.aspx.cs" Inherits="Demoexsql.DeptCotractDetai_s" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EnableViewState="true"  
  11. OnRowDataBound="GridView1_RowDataBound">  
  12. <Columns>  
  13. <asp:BoundField HeaderText="Id" DataField="CountryId" />  
  14. <asp:TemplateField HeaderText="Country">  
  15. <ItemTemplate>  
  16. <asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drpcountry_SelectedIndexChanged">  
  17. </asp:DropDownList>  
  18. </ItemTemplate>  
  19. </asp:TemplateField>  
  20. <asp:TemplateField HeaderText="State">  
  21. <ItemTemplate>  
  22. <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drpstate_SelectedIndexChanged">  
  23. </asp:DropDownList>  
  24. </ItemTemplate>  
  25. </asp:TemplateField>  
  26. <asp:TemplateField HeaderText="City/Town">  
  27. <ItemTemplate>  
  28. <asp:DropDownList runat="server" ID="ddlCity" ClientIDMode="Static" Width="100px">  
  29. </asp:DropDownList>  
  30. </ItemTemplate>  
  31. </asp:TemplateField>  
  32. </Columns>  
  33. </asp:GridView>  
  34. <br />  
  35. <asp:Button ID="Button1" Text="Save" runat="server" OnClick="Save" />  
  36. <br />  
  37. <asp:GridView ID="Gridview2" runat="server" AutoGenerateColumns="false">  
  38. <Columns>  
  39. <asp:BoundField HeaderText="Country" DataField="Country" />  
  40. <asp:BoundField HeaderText="State" DataField="State" />  
  41. <asp:BoundField HeaderText="City" DataField="City" />  
  42. </Columns>  
  43. </asp:GridView>  
  44. </div>  
  45. </form>  
  46. </body>  
  47. </html>  
.aspx.cs==
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10. namespace Demoexsql  
  11. {  
  12. public partial class DeptCotractDetai_s : System.Web.UI.Page  
  13. {  
  14. protected void Page_Load(object sender, EventArgs e)  
  15. {  
  16. if (!IsPostBack)  
  17. {  
  18. GridView1.DataSource = GetData("SELECT * FROM Countries");  
  19. GridView1.DataBind();  
  20. }  
  21. DataTable dt = new DataTable();  
  22. dt.Columns.AddRange(new DataColumn[] { new DataColumn("Country"typeof(string)), new DataColumn("State"typeof(string)), new DataColumn("City"typeof(string)) });  
  23. DropDownList ddlCountries = null;  
  24. DropDownList ddlStates = null;  
  25. DropDownList ddlCities = null;  
  26. foreach (GridViewRow row in GridView1.Rows)  
  27. {  
  28. if (row.RowType == DataControlRowType.DataRow)  
  29. {  
  30. ddlCountries = (row.FindControl("ddlCountries"as DropDownList);  
  31. ddlStates = (row.FindControl("ddlState"as DropDownList);  
  32. ddlCities = (row.FindControl("ddlCity"as DropDownList);  
  33. string country = (ddlCountries.SelectedItem.Value == "0" ? string.Empty : ddlCountries.SelectedItem.Value);  
  34. string state = (ddlStates.SelectedItem.Value == "0" ? string.Empty : ddlStates.SelectedItem.Value);  
  35. string city = (ddlCities.SelectedItem.Value == "0" ? string.Empty : ddlCities.SelectedItem.Value);  
  36. if (country != "" || state != "" || city != "")  
  37. {  
  38. dt.Rows.Add(ddlCountries.SelectedItem.Text == "Select Country" ? "NULL" : ddlCountries.SelectedItem.Text, ddlStates.SelectedItem.Text == "Select State" ? "NULL" : ddlStates.SelectedItem.Text, ddlCities.SelectedItem.Text == "Select City" ? "NULL" : ddlCities.SelectedItem.Text);  
  39. }  
  40. else  
  41. {  
  42. dt.Rows.Add(country == "" ? "NULL" : ddlCountries.SelectedItem.Value, country == "" ? "NULL" : ddlStates.SelectedItem.Value, country == "" ? "NULL" : ddlCities.SelectedItem.Value);  
  43. }  
  44. }  
  45. Gridview2.DataSource = dt;  
  46. Gridview2.DataBind();  
  47. }  
  48. }  
  49. private DataSet GetData(string query)  
  50. {  
  51. string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
  52. SqlCommand cmd = new SqlCommand(query);  
  53. using (SqlConnection con = new SqlConnection(conString))  
  54. {  
  55. using (SqlDataAdapter sda = new SqlDataAdapter())  
  56. {  
  57. cmd.Connection = con;  
  58. sda.SelectCommand = cmd;  
  59. using (DataSet ds = new DataSet())  
  60. {  
  61. sda.Fill(ds);  
  62. return ds;  
  63. }  
  64. }  
  65. }  
  66. }  
  67. private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)  
  68. {  
  69. string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
  70. SqlCommand cmd = new SqlCommand(query);  
  71. using (SqlConnection con = new SqlConnection(conString))  
  72. {  
  73. using (SqlDataAdapter sda = new SqlDataAdapter())  
  74. {  
  75. cmd.Connection = con;  
  76. con.Open();  
  77. ddl.DataSource = cmd.ExecuteReader();  
  78. ddl.DataTextField = text;  
  79. ddl.DataValueField = value;  
  80. ddl.DataBind();  
  81. con.Close();  
  82. }  
  83. }  
  84. ddl.Items.Insert(0, new ListItem(defaultText, "0"));  
  85. }  
  86. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  87. {  
  88. DropDownList ddlCountries = null;  
  89. DropDownList ddlStates = null;  
  90. DropDownList ddlCities = null;  
  91. if (e.Row.RowType == DataControlRowType.DataRow)  
  92. {  
  93. ddlCountries = (e.Row.FindControl("ddlCountries"as DropDownList);  
  94. ddlStates = (e.Row.FindControl("ddlState"as DropDownList);  
  95. ddlCities = (e.Row.FindControl("ddlCity"as DropDownList);  
  96. BindDropDownList(ddlCountries, "SELECT DISTINCT CountryName,CountryId FROM Countries""CountryName""CountryId""Select Country");  
  97. BindDropDownList(ddlStates, "SELECT DISTINCT StateName,StateId FROM States""StateName""StateId""Select Country");  
  98. BindDropDownList(ddlCities, "SELECT DISTINCT CityName,CityId FROM Cities""CityName""CityId""Select Country");  
  99. ddlStates.Enabled = false;  
  100. ddlCities.Enabled = false;  
  101. ddlStates.Items.Insert(0, new ListItem("Select State""0"));  
  102. ddlCities.Items.Insert(0, new ListItem("Select City""0"));  
  103. }  
  104. }  
  105. protected void drpcountry_SelectedIndexChanged(object sender, EventArgs e)  
  106. {  
  107. DropDownList ddlCountries = (DropDownList)sender;  
  108. GridViewRow currentRow = (GridViewRow)ddlCountries.NamingContainer;  
  109. DropDownList ddlStates = (DropDownList)currentRow.FindControl("ddlState");  
  110. DropDownList ddlCities = (DropDownList)currentRow.FindControl("ddlCity");  
  111. ddlStates.Enabled = false;  
  112. ddlCities.Enabled = false;  
  113. ddlStates.Items.Clear();  
  114. ddlCities.Items.Clear();  
  115. ddlStates.Items.Insert(0, new ListItem("Select State""0"));  
  116. ddlCities.Items.Insert(0, new ListItem("Select City""0"));  
  117. int countryId = int.Parse(ddlCountries.SelectedItem.Value);  
  118. if (countryId > 0)  
  119. {  
  120. string query = string.Format("select StateId, StateName from States where CountryId = {0}", countryId);  
  121. BindDropDownList(ddlStates, query, "StateName""StateId""Select State");  
  122. ddlStates.Enabled = true;  
  123. }  
  124. }  
  125. protected void drpstate_SelectedIndexChanged(object sender, EventArgs e)  
  126. {  
  127. DropDownList ddlCountries = (DropDownList)sender;  
  128. GridViewRow currentRow = (GridViewRow)ddlCountries.NamingContainer;  
  129. DropDownList ddlStates = (DropDownList)currentRow.FindControl("ddlState");  
  130. DropDownList ddlCities = (DropDownList)currentRow.FindControl("ddlCity");  
  131. ddlCities.Enabled = false;  
  132. ddlCities.Items.Clear();  
  133. ddlCities.Items.Insert(0, new ListItem("Select City""0"));  
  134. int stateId = int.Parse(ddlStates.SelectedItem.Value);  
  135. if (stateId > 0)  
  136. {  
  137. string query = string.Format("select CityId, CityName from Cities where StateId = {0}", stateId);  
  138. BindDropDownList(ddlCities, query, "CityName""CityId""Select City");  
  139. ddlCities.Enabled = true;  
  140. }  
  141. }  
  142. protected void Save(object sender, EventArgs e)  
  143. {  
  144. }  
  145. protected void Save(object sender, EventArgs e)  
  146. {  
  147. }  
  148. }  
  149. }  
Regards,
Indradeo

Answers (1)