GridView.Rows プロパティ
アセンブリ: System.Web (system.web.dll 内)


Rows プロパティ (コレクション) は、GridView コントロールのデータ行を格納するために使用されます。GridView コントロールは、データ ソースの各レコードの GridViewRow オブジェクトを作成し、各オブジェクトをコレクションに追加することによって、自動的に Rows コレクションにオブジェクトを読み込みます。通常、このプロパティは、コントロール内の特定の行にアクセスする場合、または行のコレクション全体を反復処理する場合に使用されます。
![]() |
---|
RowType プロパティが DataControlRowType.DataRow に設定されている行だけが Rows コレクションに格納されます。ヘッダー、フッター、およびページの各行を表す GridViewRow オブジェクトは、コレクションに含まれません。 |

Rows コレクションを使用して、GridView コントロールの編集中の行にアクセスする方法を次のコード例に示します。行が更新されると、正常に更新されたことを示すメッセージが表示されます。
<%@ Page language="VB" %> <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> <body> <form 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="C#" %> <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> <body> <form 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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からGridView.Rows プロパティを検索する場合は、下記のリンクをクリックしてください。

- GridView.Rows プロパティのページへのリンク