我刚开始学习ASP.Net。昨晚我正在研究Grid View,我通过直接向数据库发出命令来填充数据。我没有为我的网格定义任何模板字段,我已将AutoGenerated编辑按钮设置为true。数据显示正确,但在尝试处理更新事件时我遇到问题,因为我无法检索我正在更新的行的单元格值。
我google了很多,但是检索单元格值的所有示例都基于模板字段,但在我的情况下,我没有在设计时定义网格的列。 请帮帮我。
我的代码如下:(当我通过应用断点检查时,GridView1_RowUpdating方法给出空值)
private void FillGrid()
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=TestDatabase;Integrated security=true;");
DataTable dt = new DataTable("Student");
using (SqlDataAdapter adapter = new SqlDataAdapter("select name,age,Fathername,dob from student", con))
{
adapter.Fill(dt);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
FillGrid();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Response.Write("RowDataBoundEvent Occured"+" "+e.Row.RowType+"\n");
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
FillGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string name = GridView1.Rows[e.RowIndex].Cells[2].Text;
string age = GridView1.Rows[GridView1.EditIndex].Cells[3].Text;
string salary = GridView1.Rows[GridView1.EditIndex].Cells[4].Text;
}
答案 0 :(得分:0)
查看GridViewUpdateEventArgs.OldValues和GridViewUpdateEventArgs.NewValues。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string name = e.NewValues[2];
string age = e.NewValues[3];
string salary = e.NewValues[4];
}