从 DataGrid 控件进行更新 通过获取传入事件对象的行(Item 对象)的 ItemIndex 属性确定哪个 DataGrid 行已被更新。然后使用该索引值从网格的 DataKeys 集合中获取对应的值。 ' Visual Basic Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()// C# string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 从 DataGrid 行中获取更改的值。若要完成该操作,请: 从传入事件对象的项的 Cells 集合中获取适当的单元格(从零开始的)。例如,网格中最左边一列为 Cells(0)。 对于每个单元格,获取其 Controls 集合,它包含显示在该单元格中的所有元素。 从该集合中获取且仅获取第一个控件,在本例中为 TextBox 控件。若要获取 TextBox,声明一个类型 TextBox 的局部变量,并将 Controls 集合中的对象分配给它。 获取 TextBox 控件的值(其 Text 属性)。 下面的示例显示如何执行这些步骤。 ' 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(); }
按照配置,DataGrid 控件将显示 Categories 表中的信息。但您还希望用户可以逐个编辑网格中的行。要实现该目的,向网格的每一行添加一个“编辑”按钮。用户单击该按钮,网格以编辑模式重新显示,用户可以在文本框中逐个编辑列。当行处于编辑模式时,“编辑”按钮由另外两个按钮(一个“更新”按钮和一个“取消”按钮)代替,如下图所示:您可以设置 DataGrid 控件的属性显示这些按钮。但是,这些按钮不是自动建立连接的;相反,当单击“编辑”、“取消”或“更新”按钮时,它引发您响应的事件。您需要添加短事件处理程序,它们完成以下操作: “编辑”按钮 - 将当前行设置为编辑模式。
“取消”按钮 - 将当前行返回到显示模式。
“更新”按钮的事件处理程序更复杂,这是因为它执行更新,而这正是本演练的核心。阅读下一节,您将了解有关这方面的内容。向 DataGrid 控件添加编辑功能 在 Web 窗体设计器的“设计”视图中,选择 DataGrid 控件,按 F4 键显示“属性”窗口,然后单击该窗口底部的“属性生成器”。
显示“DataGrid1 属性”对话框。 单击“列”选项卡。
在“列列表”下面,在“可用列”列表中向下滚动,打开“按钮列”节点。
选择“编辑、更新、取消”并单击“添加”() 按钮将这些按钮添加到“选定的列”框中。
单击“确定”。
DataGrid 控件重新显示,同时在左侧的列中显示“编辑”链接按钮 (LinkButton)。(最初,您看不到“更新”和“取消”按钮。) 既已具有了“编辑”按钮,您需要创建事件处理程序来设置行的编辑模式。若要控制编辑模式,请将 DataGrid 控件的 EditItemIndex 属性设置为要编辑行的索引(从零开始的)。例如,若要将第三行设置为编辑模式,请将该属性设置为 2。若要某行返回到显示模式,请将该属性设置为 –1。在更改了编辑模式后,您必须重新绑定网格使其显示该行中的数据。您可以通过传递到处理程序的事件对象确定用户当前所在的行。这些事件的事件对象包含一个 Item 属性,该属性表示正在被更新的整个 DataGrid 行。Item 对象又支持多个属性,其中包含 Item.ItemIndex 属性,该属性返回您正在操作的行的索引值。设置编辑模式 (在 Visual Basic 中)右击该页并选择“查看代码”以在代码编辑器中打开该页的类文件。
- 或 - (在 Visual C# 中)在“设计”视图中,选择网格并按 F4 键打开“属性”窗口。 (在 Visual Basic 中)在代码编辑器上方的左侧下拉列表中选择“DataGrid1”。
- 或 - (在 Visual C# 中)单击“属性”窗口顶部的“事件”按钮 ()。 (在 Visual Basic 中)在代码编辑器顶部右侧的下拉列表中选择“EditCommand”。
- 或 - (在 Visual C# 中)双击“属性”窗口中的“EditCommand”。 创建了一个 DataGrid1_EditCommand 处理程序。 为 CancelCommand 事件重复第二步和第三步。
创建了一个 DataGrid1_CancelCommand 处理程序。 在 EditCommand 事件处理程序中添加下面的代码:
' Visual Basic
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()// C#
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
在 CancelCommand 事件处理程序中添加下面的代码:
' Visual Basic
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()// C#
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
' Visual Basic
Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()// C#
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
从 DataGrid 行中获取更改的值。若要完成该操作,请:
从传入事件对象的项的 Cells 集合中获取适当的单元格(从零开始的)。例如,网格中最左边一列为 Cells(0)。
对于每个单元格,获取其 Controls 集合,它包含显示在该单元格中的所有元素。
从该集合中获取且仅获取第一个控件,在本例中为 TextBox 控件。若要获取 TextBox,声明一个类型 TextBox 的局部变量,并将 Controls 集合中的对象分配给它。
获取 TextBox 控件的值(其 Text 属性)。
下面的示例显示如何执行这些步骤。 ' 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();
}
现在出现了几个编辑框,如何给他们取名字?????
mycommand.parameters[("@pdccase"].value = (textbox)e.item.findcontrol("edit_pdccase").text中("edit_pdccase")是什么东西???