GridView.Sort(String, SortDirection) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Sortiert das GridView Steuerelement basierend auf dem angegebenen Sortierausdruck und der angegebenen Richtung.
public:
virtual void Sort(System::String ^ sortExpression, System::Web::UI::WebControls::SortDirection sortDirection);
public virtual void Sort(string sortExpression, System.Web.UI.WebControls.SortDirection sortDirection);
abstract member Sort : string * System.Web.UI.WebControls.SortDirection -> unit
override this.Sort : string * System.Web.UI.WebControls.SortDirection -> unit
Public Overridable Sub Sort (sortExpression As String, sortDirection As SortDirection)
Parameter
- sortDirection
- SortDirection
Einer der SortDirection Werte.
Ausnahmen
Das GridView Steuerelement ist an ein Datenquellensteuerelement gebunden, das DataSourceView der Datenquelle nullzugeordnet ist.
Beispiele
Im folgenden Beispiel wird veranschaulicht, wie die Methode zum programmgesteuerten Sortieren des GridView Steuerelements nach mehreren Spalten verwendet Sort wird.
<%@ 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 SortButton_Click(Object sender, EventArgs e)
{
String expression = "";
SortDirection direction;
// Create the sort expression from the values selected
// by the user from the DropDownList controls. Multiple
// columns can be sorted by creating a sort expression
// that contains a comma-separated list of field names.
expression = SortList1.SelectedValue + "," + SortList2.SelectedValue;
// Determine the sort direction. The sort direction
// applies only to the second column sorted.
switch (DirectionList.SelectedValue)
{
case "Ascending":
direction = SortDirection.Ascending;
break;
case "Descending":
direction = SortDirection.Descending;
break;
default:
direction = SortDirection.Ascending;
break;
}
// Use the Sort method to programmatically sort the GridView
// control using the sort expression and direction.
CustomersGridView.Sort(expression, direction);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Sort Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Sort Example</h3>
<table>
<tr>
<td>
Sort by:
<asp:dropdownlist ID="SortList1"
runat="server">
<asp:listitem Selected="true">CustomerID</asp:listitem>
<asp:listitem>CompanyName</asp:listitem>
<asp:listitem>Address</asp:listitem>
<asp:listitem>City</asp:listitem>
<asp:listitem>PostalCode</asp:listitem>
<asp:listitem>Country</asp:listitem>
</asp:dropdownlist>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td>
Then by:
<asp:dropdownlist ID="SortList2"
runat="server">
<asp:listitem Selected="true">CustomerID</asp:listitem>
<asp:listitem>CompanyName</asp:listitem>
<asp:listitem>Address</asp:listitem>
<asp:listitem>City</asp:listitem>
<asp:listitem>PostalCode</asp:listitem>
<asp:listitem>Country</asp:listitem>
</asp:dropdownlist>
</td>
<td>
Sort order:
</td>
<td>
<asp:radiobuttonlist id="DirectionList"
runat="server">
<asp:listitem selected="true">Ascending</asp:listitem>
<asp:listitem>Descending</asp:listitem>
</asp:radiobuttonlist>
</td>
</tr>
</table>
<asp:button id="SortButton"
text="Sort"
onclick="SortButton_Click"
runat="Server"/>
<br/>
<hr/>
<br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="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="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 SortButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim expression As String = ""
Dim direction As SortDirection
' Create the sort expression from the values selected
' by the user from the DropDownList controls. Multiple
' columns can be sorted by creating a sort expression
' that contains a comma-separated list of field names.
expression = SortList1.SelectedValue & "," & SortList2.SelectedValue
' Determine the sort direction. The sort direction
' applies only to the second column sorted.
Select Case DirectionList.SelectedValue
Case "Ascending"
direction = SortDirection.Ascending
Case "Descending"
direction = SortDirection.Descending
Case Else
direction = SortDirection.Ascending
End Select
' Use the Sort method to programmatically sort the GridView
' control using the sort expression and direction.
CustomersGridView.Sort(expression, direction)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Sort Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>GridView Sort Example</h3>
<table>
<tr>
<td>
Sort by:
<asp:dropdownlist ID="SortList1"
runat="server">
<asp:listitem Selected="true">CustomerID</asp:listitem>
<asp:listitem>CompanyName</asp:listitem>
<asp:listitem>Address</asp:listitem>
<asp:listitem>City</asp:listitem>
<asp:listitem>PostalCode</asp:listitem>
<asp:listitem>Country</asp:listitem>
</asp:dropdownlist>
</td>
<td colspan="2">
</td>
</tr>
<tr>
<td>
Then by:
<asp:dropdownlist ID="SortList2"
runat="server">
<asp:listitem Selected="true">CustomerID</asp:listitem>
<asp:listitem>CompanyName</asp:listitem>
<asp:listitem>Address</asp:listitem>
<asp:listitem>City</asp:listitem>
<asp:listitem>PostalCode</asp:listitem>
<asp:listitem>Country</asp:listitem>
</asp:dropdownlist>
</td>
<td>
Sort order:
</td>
<td>
<asp:radiobuttonlist id="DirectionList"
runat="server">
<asp:listitem selected="true">Ascending</asp:listitem>
<asp:listitem>Descending</asp:listitem>
</asp:radiobuttonlist>
</td>
</tr>
</table>
<asp:button id="SortButton"
text="Sort"
onclick="SortButton_Click"
runat="Server"/>
<br/>
<hr/>
<br/>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="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>
Hinweise
Verwenden Sie die Sort Methode, um das GridView Steuerelement programmgesteuert mithilfe des angegebenen Sortierausdrucks und der angegebenen Richtung zu sortieren. Der Sortierausdruck gibt die Spalte oder Spalten an, mit denen sortiert werden soll. Um mehrere Spalten zu sortieren, erstellen Sie einen Sortierausdruck, der eine durch Trennzeichen getrennte Liste von Feldnamen enthält. Die Sortierrichtung gibt an, ob die Sortierung in aufsteigender oder absteigender Reihenfolge ausgeführt wird. Diese Methode wird häufig verwendet, wenn Sie das GridView Steuerelement von außerhalb des Steuerelements sortieren müssen, z. B. von einem anderen Steuerelement auf der Seite. Diese Methode wird auch häufig verwendet, um programmgesteuert eine Standardsortierreihenfolge für das GridView Steuerelement festzulegen, wenn es zum ersten Mal gerendert wird. Durch Aufrufen dieser Methode werden auch die Sorted Ereignisse und Sorting Ereignisse ausgelöst.