注册 登录
编程论坛 ASP.NET技术论坛

使用command对象修改数据

yangxincheng 发布于 2008-11-05 15:19, 1013 次点击
以下代码我看不太懂,请高手指教指教! 谢谢了~!
这是利用command对象修改数据的问题。把数据库中的东西用GridView显示出来,并且把GridView的AutoGerateEditButton属性设置为True。单击编辑的时候出现“更新”和“取消”。以下是“更新”的事件!

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string CName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
        string sqlStr = "update tb_Class set ClassName='" + CName + "' where ClassID=" + ClassID;
        SqlConnection myConn = GetConnection();
        myConn.Open();
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        myCmd.Dispose();
        myConn.Close();
        GridView1.EditIndex = -1;
        this.bind();
    }

比如这两句:int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            string CName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
   我基本上就不懂这是什么意思?请详细的解释以下,后面几个单词代表什么.

还有帮把这个代码都解释一下!麻烦各位了 ~谢了!(麻烦请尽量详细点!)
3 回复
#2
xuezihanfd2008-11-05 17:12
这是在GridView的事件里
  e.RowIndex是触发事件的行,  DataKeys[e.RowIndex]从事件行获得主键值ClassID
  GridView1.Rows[e.RowIndex].Cells[2].Controls[0])
      Cells【2】事件行的第二个单元格
      Controls[0] 第二个单元格里的第一个控件
   ((TextBox) 强制转换成 TextBox   然后 TextBox.Text.ToString() 获得CName
#3
yangxincheng2008-11-06 16:05
谢谢楼上的了
能说的再明白一点吗?
更详细一些!
#4
_断肠人_2008-11-08 02:02
1