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):
onrowediting="GridView1_RowEditing">
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");
}
}
}
{
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.......
Prakash MondalPosted May 21, 2014, 3:37 PM
thank you.
Nilesh AvhadPosted May 19, 2014, 3:48 AM
This may help you.
http://www.daniweb.com/web-development/aspnet/threads/284290/how-to-display-drop-down-list-item-based-on-value-selected-in-gridview