GridView.Rows 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取表示 GridViewRow 控件中数据行的 GridView 对象的集合。
public:
virtual property System::Web::UI::WebControls::GridViewRowCollection ^ Rows { System::Web::UI::WebControls::GridViewRowCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.GridViewRowCollection Rows { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Rows : System.Web.UI.WebControls.GridViewRowCollection
Public Overridable ReadOnly Property Rows As GridViewRowCollection
属性值
一个 GridViewRowCollection,包含 GridView 控件中的所有数据行。
- 属性
示例
以下示例演示如何使用 Rows 集合访问控件中正在编辑的 GridView 行。 更新行后,将显示一条消息以指示更新成功。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// Clear the message label when the user enters edit mode.
if (e.CommandName == "Edit")
{
Message.Text = "";
}
}
void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
{
// The update operation was successful. Retrieve the row being edited.
int index = CustomersGridView.EditIndex;
GridViewRow row = CustomersGridView.Rows[index];
// Notify the user that the update was successful.
Message.Text = "Updated record " + row.Cells[1].Text + ".";
}
void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
// The update operation was canceled. Display the appropriate message.
Message.Text = "Update operation canceled.";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Rows Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Rows Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="CustomersGridView"
allowpaging="true"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onrowcommand="CustomersGridView_RowCommand"
onrowupdated="CustomersGridView_RowUpdated"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
' Clear the message label when the user enters edit mode.
If e.CommandName = "Edit" Then
Message.Text = ""
End If
End Sub
Sub CustomersGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
' The update operation was successful. Retrieve the row being edited.
Dim index As Integer = CustomersGridView.EditIndex
Dim row As GridViewRow = CustomersGridView.Rows(index)
' Notify the user that the update was successful.
Message.Text = "Updated record " & row.Cells(1).Text + "."
End Sub
Sub CustomersGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
' The update operation was canceled. Display the appropriate message.
Message.Text = "Update operation canceled."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Rows Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Rows Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="CustomersGridView"
allowpaging="true"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onrowcommand="CustomersGridView_RowCommand"
onrowupdated="CustomersGridView_RowUpdated"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
deletecommand="Delete from Customers where CustomerID = @CustomerID"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
属性 Rows (集合) 用于在控件中 GridView 存储数据行。 控件GridView通过为数据源中的每个记录创建一个GridViewRow对象,然后将每个对象添加到集合,从而自动Rows填充集合。 此属性通常用于访问控件中的特定行,或者循环访问整个行集合。
备注
只有属性 RowType 设置为的 DataControlRowType.DataRow
行存储在集合中 Rows 。 GridViewRow表示页眉、页脚和寻呼行的对象不包括在集合中。