void ItemsGrid_Update(Object sender, DataGridCommandEventArgs e) { // Retrieve the text boxes that contain the values to update. // For bound columns, the edited value is stored in a TextBox. // The TextBox is the 0th control in a cell's Controls collection. // Each cell in the Cells collection of a DataGrid item represents // a column in the DataGrid control. TextBox qtyText = (TextBox)e.Item.Cells[3].Controls[0]; TextBox priceText = (TextBox)e.Item.Cells[4].Controls[0];
// With a database, use an update command to update the data. // Because the data source in this example is an in-memory // DataTable, delete the old row and replace it with a new one.
// Remove the old entry and clear the row filter. CartView.RowFilter = "Item='" + item + "'"; if (CartView.Count > 0) { CartView.Delete(0); } CartView.RowFilter = "";
// *************************************************************** // Insert data validation code here. Make sure to validate the // values entered by the user before converting to the appropriate // data types and updating the data source. // *************************************************************** // Add the new entry. dr = Cart.NewRow(); dr[0] = Convert.ToInt32(qty); dr[1] = item; // If necessary, remove the '$' character, from the price before // converting it to a Double. if(price[0] == '$') { dr[2] = Convert.ToDouble(price.Substring(1)); } else { dr[2] = Convert.ToDouble(price); } Cart.Rows.Add(dr);
// Set the EditItemIndex property to -1 to exit editing mode. // Be sure to rebind the DateGrid to the data source to refresh // the control. ItemsGrid.EditItemIndex = -1; BindGrid(); }
SqlCommand Cmd=new SqlCommand(strSql,Conn); SqlDataAdapter adapter=new SqlDataAdapter(Cmd);
adapter.Fill(ds,"righttable"); int row=Convert.ToInt32(e.Item.ItemIndex);
ds.Tables["righttable"].Rows[row].Delete(); SqlCommandBuilder objBuilder=new SqlCommandBuilder(adapter);
adapter.UpdateCommand=objBuilder.GetUpdateCommand();
adapter.Update(ds,"righttable");
sqlDataAdapter1.Update(dataSet11);
自动把DataSet中的变化过的数据写回到数据库中,就这么简单!!
{ // Retrieve the text boxes that contain the values to update.
// For bound columns, the edited value is stored in a TextBox.
// The TextBox is the 0th control in a cell's Controls collection.
// Each cell in the Cells collection of a DataGrid item represents
// a column in the DataGrid control.
TextBox qtyText = (TextBox)e.Item.Cells[3].Controls[0];
TextBox priceText = (TextBox)e.Item.Cells[4].Controls[0];
// Retrieve the updated values.
String item = e.Item.Cells[2].Text;
String qty = qtyText.Text;
String price = priceText.Text;
DataRow dr;
// With a database, use an update command to update the data.
// Because the data source in this example is an in-memory
// DataTable, delete the old row and replace it with a new one.
// Remove the old entry and clear the row filter.
CartView.RowFilter = "Item='" + item + "'";
if (CartView.Count > 0)
{
CartView.Delete(0);
}
CartView.RowFilter = "";
// ***************************************************************
// Insert data validation code here. Make sure to validate the
// values entered by the user before converting to the appropriate
// data types and updating the data source.
// *************************************************************** // Add the new entry.
dr = Cart.NewRow();
dr[0] = Convert.ToInt32(qty);
dr[1] = item; // If necessary, remove the '$' character, from the price before
// converting it to a Double.
if(price[0] == '$')
{
dr[2] = Convert.ToDouble(price.Substring(1));
}
else
{
dr[2] = Convert.ToDouble(price);
} Cart.Rows.Add(dr);
// Set the EditItemIndex property to -1 to exit editing mode.
// Be sure to rebind the DateGrid to the data source to refresh
// the control.
ItemsGrid.EditItemIndex = -1;
BindGrid(); }