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


コレクション内の項目の数を確認するには、Count プロパティを使用します。通常、Count プロパティは、コレクションを反復処理してコレクションの上限を確認する場合に使用されます。Count プロパティは、コレクションが空であるかどうかを確認する手段としてもよく使用されます。

Count プロパティを使用して、GridView コントロールにレコードが含まれているかどうかを確認する方法を次の例に示します。値が 0 より大きい場合は、GridView コントロールに 1 つ以上のレコードが含まれていることを示します。
<%@ Page language="VB" %> <script runat="server"> Sub AuthorsGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.Footer Then ' Get the number of items in the Rows collection. Dim count As Integer = AuthorsGridView.Rows.Count ' If the GridView control contains any records, display ' the last name of each author in the GridView control. If count > 0 Then Message.Text = "The authors are:<br>" Dim row As GridViewRow For Each row In AuthorsGridView.Rows Message.Text &= row.Cells(0).Text & "<br>" Next End If End If End Sub </script> <html> <body> <form runat="server"> <h3>GridViewRowCollection Example</h3> <table> <tr> <td> <asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource" autogeneratecolumns="false" onrowcreated="AuthorsGridView_RowCreated" runat="server"> <columns> <asp:boundfield datafield="au_lname" headertext="Last Name"/> <asp:boundfield datafield="au_fname" headertext="First Name"/> </columns> </asp:gridview> </td> <td> <asp:label id="Message" forecolor="Red" runat="server"/> </td> </tr> </table> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA'" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void AuthorsGridView_RowCreated(Object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Footer) { // Get the number of items in the Rows collection. int count = AuthorsGridView.Rows.Count; // If the GridView control contains any records, display // the last name of each author in the GridView control. if (count > 0) { Message.Text = "The authors are:<br>"; foreach (GridViewRow row in AuthorsGridView.Rows) { Message.Text += row.Cells[0].Text + "<br>"; } } } } </script> <html> <body> <form runat="server"> <h3>GridViewRowCollection Example</h3> <table> <tr> <td> <asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource" autogeneratecolumns="false" onrowcreated="AuthorsGridView_RowCreated" runat="server"> <columns> <asp:boundfield datafield="au_lname" headertext="Last Name"/> <asp:boundfield datafield="au_fname" headertext="First Name"/> </columns> </asp:gridview> </td> <td> <asp:label id="Message" forecolor="Red" runat="server"/> </td> </tr> </table> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_lname], [au_fname] FROM [authors] WHERE [state]='CA'" connectionstring="server=localhost;database=pubs;integrated security=SSPI" 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に収録されているすべての辞書からGridViewRowCollection.Count プロパティを検索する場合は、下記のリンクをクリックしてください。

- GridViewRowCollection.Count プロパティのページへのリンク