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

Dim instance As GridView Dim value As Boolean value = instance.AllowSorting instance.AllowSorting = value
/** @property */ public boolean get_AllowSorting () /** @property */ public void set_AllowSorting (boolean value)
並べ替え機能が有効な場合は true。それ以外の場合は false。既定値は false です。

並べ替え機能をサポートしているデータ ソース コントロールが GridView コントロールにバインドされている場合、GridView コントロールは、データ ソース コントロールの機能を利用して、自動並べ替え機能を提供します。
![]() |
---|
並べ替え機能を有効にするための要件は、データ ソースによって異なります。要件を確認するには、該当するデータ ソースのドキュメントを参照してください。 |
並べ替えを有効にするには、AllowSorting プロパティを true に設定します。並べ替えが有効になっている場合、SortExpression プロパティが設定されている各列フィールドの見出しテキストがリンク ボタンとして表示されます。
![]() |
---|
自動生成された列フィールドの SortExpression プロパティは、自動的に設定されます。Columns コレクションを使用して独自の列を定義する場合は、列ごとに SortExpression プロパティを設定する必要があります。そうしないと、列のヘッダーにリンク ボタンが表示されません。 |
列のリンク ボタンをクリックすると、GridView コントロール内の項目が並べ替え式に基づいて並べ替えられます。通常、並べ替え式は、単に列に表示されるフィールドの名前です。これにより、その列を基準として GridView コントロールで並べ替えが実行されます。複数のフィールドで並べ替えを実行するには、コンマ区切りのフィールド名リストを含んだ並べ替え式を使用します。SortExpression プロパティを使用して、GridView コントロールに適用される並べ替え式を確認できます。列のリンク ボタンを繰り返しクリックすると、並べ替えの方向が昇順と降順の間で切り替わります。現在の並べ替えの方向を確認するには、SortDirection プロパティを使用します。
GridView コントロールには、並べ替えが発生したときにカスタム アクションを実行するために使用できる複数のイベントが用意されています。使用できるイベントの一覧を次の表に示します。

AllowSorting プロパティを使用して、自動生成された列が使用されている場合に GridView コントロールで並べ替えを有効にする方法を次のコード例に示します。
<%@ Page language="VB" %> <html> <body> <form runat="server"> <h3>GridView AllowSorting Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="true" emptydatatext="No data available." allowsorting="true" 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="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>GridView AllowSorting Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="true" emptydatatext="No data available." allowsorting="true" 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="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
AllowSorting プロパティを使用して、Columns コレクションが定義されている場合に GridView コントロールで並べ替えを有効にする方法を次のコード例に示します。プログラムによって、並べ替えられる列のヘッダーに並べ替えの方向を指定するイメージも追加されます。この例を使用するには、独自のイメージを用意する必要があります。
<%@ Page language="VB" %> <script runat="server"> Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) ' Use the RowType property to determine whether the ' row being created is the header row. If e.Row.RowType = DataControlRowType.Header Then ' Call the GetSortColumnIndex helper method to determine ' the index of the column being sorted. Dim sortColumnIndex As Integer = GetSortColumnIndex() If sortColumnIndex <> -1 Then ' Call the AddSortImage helper method to add ' a sort direction image to the appropriate ' column header. AddSortImage(sortColumnIndex, e.Row) End If End If End Sub ' This is a helper method used to determine the index of the ' column being sorted. If no column is being sorted, -1 is returned. Function GetSortColumnIndex() As Integer ' Iterate through the Columns collection to determine the index ' of the column being sorted. Dim field As DataControlField For Each field In CustomersGridView.Columns If field.SortExpression = CustomersGridView.SortExpression Then Return CustomersGridView.Columns.IndexOf(field) End If Next Return -1 End Function ' This is a helper method used to add a sort direction ' image to the header of the column being sorted. Sub AddSortImage(ByVal columnIndex As Integer, ByVal row As GridViewRow) ' Create the sorting image based on the sort direction. Dim sortImage As New Image() If CustomersGridView.SortDirection = SortDirection.Ascending Then sortImage.ImageUrl = "~/Images/Ascending.jpg" sortImage.AlternateText = "Ascending Order" Else sortImage.ImageUrl = "~/Images/Descending.jpg" sortImage.AlternateText = "Descending Order" End If ' Add the image to the appropriate header cell. row.Cells(columnIndex).Controls.Add(sortImage) End Sub </script> <html> <body> <form runat="server"> <h3>GridView AllowSorting Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="false" emptydatatext="No data available." allowsorting="true" onrowcreated="CustomersGridView_RowCreated" runat="server"> <columns> <asp:boundfield datafield="CustomerID" headertext="Customer ID" headerstyle-wrap="false" sortexpression="CustomerID"/> <asp:boundfield datafield="CompanyName" headertext="CompanyName" headerstyle-wrap="false" sortexpression="CompanyName"/> <asp:boundfield datafield="Address" headertext="Address" headerstyle-wrap="false" sortexpression="Address"/> <asp:boundfield datafield="City" headertext="City" headerstyle-wrap="false" sortexpression="City"/> <asp:boundfield datafield="PostalCode" headertext="Postal Code" headerstyle-wrap="false" sortexpression="PostalCode" /> <asp:boundfield datafield="Country" headertext="Country" headerstyle-wrap="false" sortexpression="Country"/> </columns> </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="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e) { // Use the RowType property to determine whether the // row being created is the header row. if (e.Row.RowType == DataControlRowType.Header) { // Call the GetSortColumnIndex helper method to determine // the index of the column being sorted. int sortColumnIndex = GetSortColumnIndex(); if (sortColumnIndex != -1) { // Call the AddSortImage helper method to add // a sort direction image to the appropriate // column header. AddSortImage(sortColumnIndex, e.Row); } } } // This is a helper method used to determine the index of the // column being sorted. If no column is being sorted, -1 is returned. int GetSortColumnIndex() { // Iterate through the Columns collection to determine the index // of the column being sorted. foreach (DataControlField field in CustomersGridView.Columns) { if (field.SortExpression == CustomersGridView.SortExpression) { return CustomersGridView.Columns.IndexOf(field); } } return -1; } // This is a helper method used to add a sort direction // image to the header of the column being sorted. void AddSortImage(int columnIndex, GridViewRow headerRow) { // Create the sorting image based on the sort direction. Image sortImage = new Image(); if (CustomersGridView.SortDirection == SortDirection.Ascending) { sortImage.ImageUrl = "~/Images/Ascending.jpg"; sortImage.AlternateText = "Ascending Order"; } else { sortImage.ImageUrl = "~/Images/Descending.jpg"; sortImage.AlternateText = "Descending Order"; } // Add the image to the appropriate header cell. headerRow.Cells[columnIndex].Controls.Add(sortImage); } </script> <html> <body> <form runat="server"> <h3>GridView AllowSorting Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSource" autogeneratecolumns="false" emptydatatext="No data available." allowsorting="true" onrowcreated="CustomersGridView_RowCreated" runat="server"> <columns> <asp:boundfield datafield="CustomerID" headertext="Customer ID" headerstyle-wrap="false" sortexpression="CustomerID"/> <asp:boundfield datafield="CompanyName" headertext="CompanyName" headerstyle-wrap="false" sortexpression="CompanyName"/> <asp:boundfield datafield="Address" headertext="Address" headerstyle-wrap="false" sortexpression="Address"/> <asp:boundfield datafield="City" headertext="City" headerstyle-wrap="false" sortexpression="City"/> <asp:boundfield datafield="PostalCode" headertext="Postal Code" headerstyle-wrap="false" sortexpression="PostalCode" /> <asp:boundfield datafield="Country" headertext="Country" headerstyle-wrap="false" sortexpression="Country"/> </columns> </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="CustomersSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </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.AllowSorting プロパティを検索する場合は、下記のリンクをクリックしてください。

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