private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string  email, Name, Compony, personPosition, department, homePhone, mobilePhone, officePhone;

string key=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
TextBox tb;
tb = (TextBox)(e.Item.Cells[0].Controls[0]);
Name = tb.Text;
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
email = tb.Text;
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
Compony=tb.Text;
tb = (TextBox)(e.Item.Cells[4].Controls[0]);
personPosition=tb.Text;
tb = (TextBox)(e.Item.Cells[5].Controls[0]);
department=tb.Text;
tb = (TextBox)(e.Item.Cells[6].Controls[0]);
homePhone=tb.Text; tb = (TextBox)(e.Item.Cells[7].Controls[0]);
mobilePhone=tb.Text;
            
tb = (TextBox)(e.Item.Cells[8].Controls[0]);
officePhone=tb.Text;
   

DataSet1.PersonRow r;
r=dataSet11.Person.FindById(int.Parse(key)); // Updates the dataset table.
r.compony=Compony;
r.department=department;
r.mobilePhone=mobilePhone;
            r.officePhone=officePhone;
r.homePhone=homePhone;
r.personPosition=personPosition;
r.Name =Name;
r.Email = email; // Calls a SQL statement to update the database from the dataset
sqlDataAdapter1.Update(dataSet11); // Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1; // Refreshes the grid
DataGrid1.DataBind();
}

解决方案 »

  1.   

    private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex=e.Item.ItemIndex;
    DataGrid1.DataBind();
    } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex = -1;
    DataGrid1.DataBind();
    }
      

  2.   

    在DataGrid1_UpdateCommand事件中,把第一句改为如下试试;
    string key=DataGrid1.DataKeys[(int)DataGrid1.EditItemIndex].ToString();
      

  3.   

    应该是小问题,我赞成chinaewway的方法,很好!