我有一个gridview和其中一行的下拉列表,我能够成功从数据库中检索值,但是当更改和更新时,我无法更新下拉值。
我的C#文件是:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddList = (DropDownList)e.Row.FindControl("ddlstatus");
DataRowView dr = e.Row.DataItem as DataRowView;
ddList.SelectedItem.Text = dr["status"].ToString();
ddList.SelectedValue = dr["status"].ToString();
// Response.Write("the selected value is"+ddList.SelectedValue);
val = e.Row.Cells[1].Text.ToString();
int id = int.Parse(val);
//Response.Write(val);
string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE driver SET status = @status WHERE driverid = @driverid"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@status", ddList.SelectedValue);
cmd.Parameters.AddWithValue("@driverid", id);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
}
}
请帮助/指导,谢谢
答案 0 :(得分:0)
我找到了解决方案,这是代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlCurrentDropDownList = (DropDownList)sender;
GridViewRow grdrDropDownRow = ((GridViewRow)ddlCurrentDropDownList.Parent.Parent);
DropDownList ddList = (DropDownList)grdrDropDownRow.FindControl("ddlstatus");
statusval = ddList.SelectedValue.ToString();
Session[statusval] = statusval;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
string rname = (string)row.Cells[1].Text;
string ConnectionStringn = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnectionStringn))
{
using (SqlCommand cmd = new SqlCommand("UPDATE driver SET status=@status WHERE driverid=@driverid"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@status",statusval);
cmd.Parameters.AddWithValue("@driverid",rname);
con.Open();
cmd.ExecuteNonQuery();
}
}
}