中国开发网: 论坛: 程序员情感CBD: 贴子 65245
咔波碧宝
MSDN 演练:使用 DataGrid Web 控件读取和写入数据 ,只不过这里用的是sqlDataAdapter
// C#
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string categoryName, categoryDescription;

// Gets the value of the key field of the row being updated
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

// Gets get the value of the controls (textboxes) that the user
// updated. The DataGrid columns are exposed as the Cells collection.
// Each cell has a collection of controls. In this case, there is only one
// control in each cell -- a TextBox control. To get its value,
// you copy the TextBox to a local instance (which requires casting)
// and extract its Text property.
//
// The first column -- Cells(0) -- contains the Update and Cancel buttons.
TextBox tb;

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[2].Controls[0]);
categoryName = tb.Text;

// Gets the value the TextBox control in the fourth column
tb = (TextBox)(e.Item.Cells[3].Controls[0]);
categoryDescription = tb.Text;

// Finds the row in the dataset table that matches the
// one the user updated in the grid. This example uses a
// special Find method defined for the typed dataset, which
// returns a reference to the row.
dsCategories.CategoriesRow r;
r = dsCategories1.Categories.FindByCategoryID(int.Parse(key));

// Updates the dataset table.
r.CategoryName = categoryName;
r.Description = categoryDescription;

// Calls a SQL statement to update the database from the dataset
sqlDataAdapter1.Update(dsCategories1);

// Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1;

// Refreshes the grid
DataGrid1.DataBind();

I think I m cute.
I know I m sexy.
I'm just a sexy boy.
凸╰_╯凸

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录