}
protected void EditCustomer(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
BindData();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCustomers.EditIndex = -1;
BindData();
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvCustomers.EditIndex == e.Row.RowIndex)
{
DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities");
string query = "select distinct city from customers";
SqlCommand cmd = new SqlCommand(query);
ddlCities.DataSource = GetData(cmd);
ddlCities.DataTextField = "city";
ddlCities.DataValueField = "city";
ddlCities.DataBind();
ddlCities.Items.FindByValue((e.Row.FindControl("lblCity") as Label).Text).Selected = true;
}
}
protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
string city = (gvCustomers.Rows[e.RowIndex].FindControl("ddlCities") as DropDownList).SelectedItem.Value;
string customerId = gvCustomers.DataKeys[e.RowIndex].Value.ToString();
string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
string query = "update customers set city = @city where customerId = @customerId";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@city", city);
cmd.Parameters.AddWithValue("@customerId", customerId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
}
}