下面的示例显示如何执行这些步骤。
' Visual Basic
Dim categoryName, categoryDescription As String
Dim tb As TextBox
tb = CType(e.Item.Cells(2).Controls(0), TextBox)
categoryName = tb.Text
tb = CType(e.Item.Cells(3).Controls(0), TextBox)
categoryDescription = tb.Text
// C#
string categoryName;
string categoryDescription;
TextBox tb;
tb = (TextBox) e.Item.Cells[2].Controls[0];
categoryName = tb.Text;
tb = (TextBox) e.Item.Cells[3].Controls[0];
categoryDescription = tb.Text
在数据表中查找对应的行。类型化的 dsCategories 数据集包含一个特殊的 FindBy 方法(在本例中为 FindByCategoryID 方法),该方法通过行的主键定位行,并返回一个对行的引用。创建类型化数据行的变量并调用该方法: ' Visual Basic
Dim r As dsCategories.CategoriesRow
r = DsCategories1.Categories.FindByCategoryID(key)
// C#
dsCategories.CategoriesRow r;
r = dsCategories1.Categories.FindByCategoryID(int.Parse(key));
通过更改您在第三步所在行中的值更新该行,如下面的示例所示: ' Visual Basic
r.CategoryName = categoryName
r.Description = categoryDescription
// C#
r.CategoryName = categoryName;
r.Description = categoryDescription;
通过调用数据适配器的 Update 方法将更改从数据集发送到数据库: ' Visual Basic
SqlDataAdapter1.Update(DsCategories1)
DataGrid1.DataBind()
// C#
sqlDataAdapter1.Update(dsCategories1);
DataGrid1.DataBind();
将网格中的当前行切换出编辑模式。 ' Visual Basic
DataGrid1.EditItemIndex = -1
// C#
DataGrid1.EditItemIndex = -1;
数据绑定 DataGrid 控件: ' Visual Basic
DataGrid1.DataBind()
// C#
DataGrid1.DataBind();
下面的代码显示完成的 UpdateCommand 事件处理程序是什么样的。复制该代码并将其粘贴到 Web 窗体页的类文件。
提示 一定要改写先前创建的主干事件处理程序,否则将有两个方法具有相同的名称和签名。
' Visual Basic
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim categoryName, categoryDescription As String
' Gets the value of the key field of the row being updated
Dim key As String = 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.
Dim tb As TextBox
' Gets the value the TextBox control in the third column
tb = CType(e.Item.Cells(2).Controls(0), TextBox)
categoryName = tb.Text
' Gets the value the TextBox control in the fourth column
tb = CType(e.Item.Cells(3).Controls(0), TextBox)
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.
Dim r As dsCategories.CategoriesRow
r = DsCategories1.Categories.FindByCategoryID(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()
End Sub
// 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();
}
测试
现在您已完成所有操作。若要阐释网格如何工作并确保它正确更新数据,您应该对页进行测试。
对页进行测试
- 在解决方案资源管理器中,右击 Web 窗体页并选择“在浏览器中查看”。
当前项目将被编译,Web 窗体页将显示在设计器的浏览器窗格中。
- 单击网格中任何一行的“编辑”链接,然后使用文本框编辑该行。
- 单击“更新”。
网格重新显示,并带有您的更改。如果检查数据库,您将看到已适当写入更改。
本文出自 51CTO.COM技术博客