How to add value into DropDownList within GridView?

Posted by prakashmondal5-21103 under ASP.NET on 5/17/2014 | Points: 10 | Views : 609 | Status : [Member] | Replies : 1
I want to add value into DropDownList based on another DropDownList within GridView. But did not work properly.

Default2.aspx (Source View):

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Operations">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="UPDATE">UPDATE</asp:LinkButton>
&nbsp;<asp:LinkButton ID="LinkButton4" runat="server" CommandName="CANCEL">CANCEL</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="EDIT">EDIT</asp:LinkButton>
&nbsp;
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="DELETE">DELETE</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Day">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem>-Select-</asp:ListItem>
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Day") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("Time") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Default2.aspx.cs (Code View):

public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\TEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select *from TestTB", con);
DataTable DDT = new DataTable();
da.Fill(DDT);
con.Close();
GridView1.DataSource = DDT;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DDLDAY = (DropDownList)GridView1.FindControl("DropDownList2");
DDLDAY = (DropDownList)sender;
string day = DDLDAY.SelectedItem.Text;
int dayindex = DDLDAY.SelectedIndex;
if (dayindex == 0)
{
MessageBox.Show("Please Choose Day.", "Wrong Selection!", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{
DropDownList DDLTIME = (DropDownList)GridView1.FindControl("DropDownList3");
DDLTIME = (DropDownList)sender;
// DDLTIME.Enabled = false;
DDLTIME.Items.Cle




Responses

Posted by: Chandradev819 on: 5/19/2014 [Member] Starter | Points: 25

Up
0
Down
Hi

You can do like this

http://www.aspdotnet-suresh.com/2011/11/how-to-implement-cascading-dropdownlist.html

Thanks and Regards
Chandradev
My Blog

prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response