hi everyone i have two problems in the above code-
1. on click of update button of gridview, dropdownlist of subcategory should take null value and should display none which is not happening.
2. on click of edit button of gridview subcategory dropdownlist is not displaying the selected value instead it displays none
Please Help!!!
//Declaring variables globally
Boolean categorySelected = false;
Boolean SubCategorySelected = false;
// Editing Row of Gridview
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
//Setting global variable value false on click of edit
categorySelected = false;
SubCategorySelected = false;
Display();
// Refresh The Data
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
//Throw Exception
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
//At the click of edit button retaining the value at postback of SubCategory
protected void DDL2_PreRender(object sender, EventArgs e)
{
try
{
if (SubCategorySelected == false)
{
for (int j = 0; j < GridView1.Rows.Count; j++)
{
//SubCategoryId is stored in hidden field of Edit Template
if (((HtmlInputHidden)GridView1.Rows[j].Cells[5].FindControl("SubCategoryIDForEdit1")) != null)
{
((DropDownList)sender).SelectedValue = ((HtmlInputHidden)GridView1.Rows[j].Cells[5].FindControl("SubCategoryIDForEdit1")).Value;
//((HtmlInputHidden)GridView1.Rows[j].Cells[4].FindControl("CategoryIDForEdit2")).Value = ((HtmlInputHidden)GridView1.Rows[j].Cells[5].FindControl("SubCategoryIDForEdit1")).Value;
}
}
}
}
//Throw Exception
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
// Updating Row of Gridview
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
e.Cancel = true;
SqlCommand cmd = new SqlCommand("UpdateRow", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
//Parsing Id of value SpecialDayId which is in hiddern field to stored procedure of UpdateRow
cmd.Parameters.Add("@SpecialDayId ", SqlDbType.Decimal).Value = Convert.ToDecimal(((HtmlInputHidden)GridView1.Rows[e.RowIndex].Cells[3].Controls[1]).Value);
//Parsing Id of value CategoryId to stored procedure of UpdateRow-
cmd.Parameters.Add("@CategoryId", SqlDbType.Int).Value = Convert.ToInt32(((HtmlInputHidden)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]).Value);
//Parsing Id of value SubCategoryId to stored procedure of UpdateRow
if (((HtmlInputHidden)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]).Value == "None")
{
cmd.Parameters.Add("@SubCategoryId", SqlDbType.Int).Value = null;
}
else
{
cmd.Parameters.Add("@SubCategoryId", SqlDbType.Int).Value = Convert.ToInt32(((HtmlInputHidden)GridView1.Rows[e.RowIndex].Cells[5].Controls[1]).Value);
}
cmd.ExecuteNonQuery();
Display();
con.Close();
// Refresh The Data
GridView1.EditIndex = -1;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
//At Selected Index Change of Category dropdownlist of gridview populating SubCategory dropdownlist
protected void DDL1_SelectedIndexChange(object sender, EventArgs e)
{
try
{
categorySelected = true;
for (int intRowIndex = 0; intRowIndex < GridView1.Rows.Count; intRowIndex++)
{
DropDownList ddl = ((DropDownList)GridView1.Rows[intRowIndex].Cells[4].FindControl("DropDownList2")) as DropDownList;
if (ddl != null)
{
ddl.Items.Clear();
SqlCommand cmd = new SqlCommand("MapSubCategory", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader DDLValues;
cmd.Parameters.Add("@CategoryId", SqlDbType.Int).Value = Convert.ToInt32(((DropDownList)GridView1.Rows[intRowIndex].Cells[4].FindControl("DropDownList1")).SelectedValue);
DDLValues = cmd.ExecuteReader();
ddl.Items.Add("None");
ddl.Items[ddl.Items.Count - 1].Selected = true;
while (DDLValues.Read())
{
ddl.Items.Add(((string)DDLValues["SubCategoryName"]));
ddl.Items[ddl.Items.Count - 1].Value = Convert.ToString((int)(DDLValues["SubCategoryId"]));
}
con.Close();
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
//Setting Selected Value of SubCategory DropDownList of gridview
protected void DDL2_SelectedIndexChange(object sender, EventArgs e)
{
try
{
SubCategorySelected = true;
//Parsing DropDownList value to hidden field
for (int RowIndex = 0; RowIndex < GridView1.Rows.Count; RowIndex++)
{
((HtmlInputHidden)GridView1.Rows[RowIndex].Cells[5].Controls[1]).Value = ((DropDownList)sender).SelectedValue;
}
//code for showing sub category value at edit click
if (SubCategorySelected == false)
{
for (int j = 0; j < GridView1.Rows.Count; j++)
{
//SubCategoryId is stored in hidden field of Edit Template
if (((HtmlInputHidden)GridView1.Rows[j].Cells[5].FindControl("SubCategoryIDForEdit1")) != null)
{
//((DropDownList)sender).SelectedValue = ((HtmlInputHidden)GridView1.Rows[j].Cells[5].FindControl("SubCategoryIDForEdit1")).Value;
((HtmlInputHidden)GridView1.Rows[j].Cells[4].FindControl("CategoryIDForEdit2")).Value = ((HtmlInputHidden)GridView1.Rows[j].Cells[5].FindControl("SubCategoryIDForEdit1")).Value;
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}