http://lucky_elove.www1.dotnetplayground.com/ShowDetail.aspx?id=8ADE535F-AD40-4DE3-A962-A64B4FAF12C4

解决方案 »

  1.   

    <asp:datagrid id="MyDg" style="Z-INDEX: 101; LEFT: 10px; POSITION: absolute; TOP: 113px" runat="server" BorderStyle="None" Font-Size="X-Small" CellPadding="2" BackColor="White" BorderWidth="1px" BorderColor="#CCCCCC" OnCancelCommand="MyDg_cancle" OnEditCommand="MyDg_edit" OnUpdateCommand="MyDg_update" OnDeleteCommand="MyDg_delete" AutoGenerateColumns="False">//看一下,不需拷贝。
    你要在程序里写MyDg_update方法的,还有你的报错是因为你没有具体绑定数据列吧,也就是说你并没有Columns集合,你看一下你的DataGrid的属性生成器里,是不是"运行时自动生成列",如果是的话,你最好是把他去掉,自己绑定(用绑定列)!
    帖一下我的,仅做参考,我的是能实现:)
    public void MyDg_update(objectSender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       TextBox currentTextBox = new TextBox();
       currentTextBox=(TextBox)e.Item.Cells[1].Controls[0];
    string updatecmd="update staff_inf set name='"+ currentTextBox.Text + "' where no='" + e.Item.Cells[2].Text + "'";
       SqlCommand ucmd=new SqlCommand(updatecmd,con);
       con.Open();
       MyDg.DataSource=ucmd.ExecuteReader();
       MyDg.DataBind();
        //返回"编辑记录"状态,绑定DataGrid!
       MyDg.EditItemIndex=-1;
       con.Close(); 
       BindGrid();//绑定方法。
    }
      

  2.   

    谢谢楼上的这位大哥。
    我现在发现我的问题是点击Update和Cancel的时候,居然触发的是Edit_Command事件。而事件肯定是错绑定错。不知道为什么会有这种现象?我都快愁死了。
      

  3.   

    你的Update和Cancel事件都是指向Edit_Command吧。
    看看是不是!
      

  4.   

    真是见鬼了,我把那个该死的datagrid删掉,又重新添加了一个,结果什么问题都没有了。
    困扰了我快一个礼拜的问题,就这么莫名其妙的解决了。感谢大家的帮助,结贴了。