1

我刚开始学习 ASP.Net。昨晚我正在研究网格视图,我通过直接向数据库发出命令来填充数据。我没有为我的网格定义任何模板字段,并且我已将 AutoGenerated 编辑按钮设置为 true。数据显示正确,但在尝试处理更新事件时我遇到了问题,因为我无法检索我正在更新的行的单元格值。

我搜索了很多,但所有检索单元格值的示例都基于模板字段,但在我的情况下,我没有在设计时定义网格的列。请帮我。

我的代码如下:(当我通过应用断点进行检查时,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;
        }
4

1 回答 1

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];
 }
于 2012-06-24T20:40:44.217 回答