Prakash Mondal

Prakash Mondal

  • NA
  • 288
  • 208.1k

How to add value into DropDownList within GridView?

May 15 2014 9:36 AM
I want to add value into DropDownList based on another DropDownList within GridView. But did not work properly.
 
Database:
 
Default2.aspx (Design View)
 
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.Clear();
DDLTIME.Items.Add("-Select-");
DDLTIME.Items.Add("08:00-10:00");
DDLTIME.Items.Add("10:00-12:00");
DDLTIME.Items.Add("12:30-02:30");
DDLTIME.Items.Add("02:30-04:30");
DDLTIME.Items.Add("05:00-07:00");

}

}

}
Output 1:
 
 
Output 2: for Edit
 
 
 Output 3 :
After Select any value of Day DropDownList then Add time in that DropDownList.
 
I want to store Time value that is (08:00-10:00 , 10:00-12:00 , ............) to Next DropDownList under the Time.
 
Please Help me.......
 
 
 

Answers (2)