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 會自動填 Rows 入集合,方法是為數據源中的每個記錄建立 GridViewRow 物件,然後將每個物件新增至集合。 此屬性通常用來存取 控制項中的特定資料列,或逐一查看整個資料列集合。
注意
只有其 RowType 屬性設定 DataControlRowType.DataRow
為 的資料列會儲存在集合中 Rows 。
GridViewRow代表頁首、頁尾和呼叫器資料列的物件不會包含在集合中。