看看我的吧,用C#写的,已成功运用。
public void Grid1_Update(Object sender, DataGridCommandEventArgs e)
    {
    //string k= Grid1.DataKeys[(int)e.Item.ItemIndex].ToString();
         string SqlUpdate="UPDATE company SET city = @city,comname = @comname,contant = @contant,email = @email,tel = @tel,fax = @fax,account = @account,memo = @memo where id = @id";
 SqlCommand MySqlCmd = new SqlCommand(SqlUpdate, MyConn);
 MySqlCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.NVarChar, 11));
 MySqlCmd.Parameters.Add(new SqlParameter("@city", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@comname", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@contant", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@tel", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@fax", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@account", SqlDbType.NVarChar, 20));
 MySqlCmd.Parameters.Add(new SqlParameter("@memo", SqlDbType.NVarChar, 20));
         MySqlCmd.Parameters["@id"].Value = Grid1.DataKeys[(int)e.Item.ItemIndex];
 string[] cols= {"@city","@comname","@contant","@email","@tel","@fax","@account","@memo"};        int numCols = e.Item.Cells.Count;
        for (int i=0; i<numCols-3; i++) {
            
            MySqlCmd.Parameters[cols[i]].Value = ((TextBox)e.Item.Cells[i+1].Controls[0]).Text;
        }
        
        MySqlCmd.Connection.Open();
        MySqlCmd.ExecuteNonQuery();
Grid1.EditItemIndex = -1;
        MySqlCmd.Connection.Close();        BindGrid();
    }
-------------------------------------------------------------
 <asp:DataGrid id="Grid1" AutoGenerateColumns="false"
                         AlternatingItemStyle-BackColor="#ffff00"
 HeaderStyle-BackColor="#aaee00"
 Width="95%"
 BorderWidth="1"
 Align="Center"
 AllowPaging="True" 
 PageSize="12" 
 PagerStyle-Mode="NumericPages" 
 PagerStyle-HorizontalAlign="Center"
 OnPageIndexChanged="Grid1_Page1" 
 OnDeleteCommand="Grid1_Delete"
 OnEditCommand="Grid1_Edit"
 OnUpdateCommand="Grid1_Update"
                                 OnCancelCommand="Grid1_Cancel" 
 BorderColor="black" 
 GridLines="Both"
 DataKeyField="id"
 runat="server">            <Columns>
    <asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="更新"  ItemStyle-Wrap="false"/>
                <asp:BoundColumn HeaderText="城市" SortExpression="city" DataField="city" />
<asp:BoundColumn HeaderText="公司名称" SortExpression="comname" DataField="comname" />
                <asp:BoundColumn HeaderText="联系人" SortExpression="contant" DataField="contant" />
                <asp:BoundColumn HeaderText="EMAIL" SortExpression="email" DataField="email" />
                <asp:BoundColumn HeaderText="TEL" SortExpression="tel" DataField="tel" />
                <asp:BoundColumn HeaderText="FAX" SortExpression="fax" DataField="fax" />
                <asp:BoundColumn HeaderText="帐户" SortExpression="account" DataField="account" />
                <asp:BoundColumn HeaderText="备注" SortExpression="memo" DataField="memo" />
        <asp:HyperLinkColumn
                  DataNavigateUrlField="id"
                  DataNavigateUrlFormatString="editresume.aspx?id={0}"
                  Text="简介"/>
               <asp:ButtonColumn Text="删除" CommandName="Delete"/>
  </Columns>

解决方案 »

  1.   

    说实话,没有谁会看你的那么长的代码的,我给你一个具体的例子,你自己看看吧。希望对你有帮助
    if (Page.IsValid)
    {
    String updateCmd = "UPDATe Hr_GZ_DD_Jngz SeT FL_Ms = @fl_ms, FL_Ms_Value = @fl_ms_value where Jngz_dd_Id = @jngz_Id";SqlCommand myCommand = new SqlCommand(updateCmd, con.link1);//con.link1为与数据库的连接串,相当你的SqlConnection
    myCommand.Parameters.Add(new SqlParameter("@jngz_Id", SqlDbType.Int, 4));
    myCommand.Parameters.Add(new SqlParameter("@fl_ms", SqlDbType.VarChar, 100));
    myCommand.Parameters.Add(new SqlParameter("@fl_ms_value", SqlDbType.VarChar,100 ));

    myCommand.Parameters["@jngz_Id"].Value = JngzDataGrid.DataKeys[(int)e.Item.ItemIndex];
    myCommand.Parameters["@fl_ms"].Value =((TextBox)e.Item.FindControl("edit_FL_Ms")).Text;
    myCommand.Parameters["@fl_ms_value"].Value =(((TextBox)e.Item.FindControl("edit_FL_Ms_Value")).Text);//myCommand.Connection.Open();
    try
    {

    myCommand.ExecuteNonQuery();
    JngzDataGrid.EditItemIndex = -1;
    }
    }
      

  2.   

    :)
    因为我们过来了:)
    更新的步骤:
    第一不:
    设定选择项,进入编辑状态;
    第二步:
    更新数据,包括:
    获得更新后的数据,此时要注意是否在获取数据之前重新绑定或填充了数据;
    将更新后的数据提交入库;
    更新datagrid
      

  3.   

    你不要设置虚拟的变量来实现,可以在dg属性中DataKeyField="id"
    然后在选出的时候select……where id="+ DataGrid1.DataKeys(id)就可以了。