为什么我在行的编辑状态下,想取更改过的某个单元格的内容总是取不到,而是取到变化前的内容呢?
public partial class ConfigWeb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
getdata();
}
public void getdata()
{
SqlConnection cn = new SqlConnection(@"Data Source=mobile;Initial Catalog=Maintain;Persist Security Info=True;User ID=sa;Password=56ssddf"));
SqlCommand cmd = new SqlCommand("select * from BusinessConfig", cn);
//SqlDataReader dr = cmd.ExecuteReader();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "BusinessConfig");
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
//GridView1.PageIndex=
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex]["ID"]);//将正在编辑的这一行与对应的数据库中的那一行的ID取出
string TaskName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
string SourcePath = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string DestPath = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
string Frequency=((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
string Suffix = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
string CityName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString();
Response.Write(CityName);
string updatesql = "UPDATE BusinessConfig SET TaskName ='" + TaskName + "',SourceFilePath='" + SourcePath + "',DestFilePath ='" + DestPath + "',Frquency ='" + Frequency + "',Suffix ='" + Suffix + "' ,ProvinceName ='" + CityName + "' WHERE ID=" + id.ToString();
//string selectsql="select TaskName,SourceFilePath,DestFilePath,Frquency,Suffix,ProvinceName from BusinessConfig ";
SqlConnection cn = new SqlConnection(@"Data Source=mobile;Initial Catalog=Maintain;Persist Security Info=True;User ID=sa;Password=56ssddf");
cn.Open();
SqlCommand cmd = new SqlCommand(updatesql, cn);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
getdata();
}
}
调试一下就知道了啊。。。。。。。。。。
if (!IsPostBack)
getdate();
有可能是事件的执行顺序导致的
Debug一下,看看是哪一步没有取到值。
(TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0])这个对象你有没有?
调试看看。