有模板列的时候,最好还是用findcontrol

解决方案 »

  1.   

    String updateCmd = "UPDATE tongxunlu SET name=@Name,hphone=@Hphone,ophone=@Ophone,mphone=@Mphone,danwei=@Danwei,adress=@Adress where id =@Id";
    SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);

    myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int, 4));
    myCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.Char));
    myCommand.Parameters.Add(new SqlParameter("@Hphone", SqlDbType.Char));
    myCommand.Parameters.Add(new SqlParameter("@Ophone", SqlDbType.Char));
    myCommand.Parameters.Add(new SqlParameter("@Mphone", SqlDbType.Char));
    myCommand.Parameters.Add(new SqlParameter("@Danwei", SqlDbType.Char));
    myCommand.Parameters.Add(new SqlParameter("@Adress", SqlDbType.Char));

    myCommand.Parameters["@Id"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
    String[] cols = {"@Name","@Hphone","@Ophone","@Mphone","@Danwei","@Adress"};
    int numCols = e.Item.Cells.Count;
    for (int i=2; i<numCols; i++) //跳过第一、第二列
    {
    String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text; myCommand.Parameters[cols[i-2]].Value = colvalue;
    }

    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close(); 
    DataGrid1.EditItemIndex = -1;
    BindGrid();