GridView.AllowSorting Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá nebo nastaví hodnotu označující, zda je povolena funkce řazení.
public:
virtual property bool AllowSorting { bool get(); void set(bool value); };
public virtual bool AllowSorting { get; set; }
member this.AllowSorting : bool with get, set
Public Overridable Property AllowSorting As Boolean
Hodnota vlastnosti
trueje-li povolena funkce řazení; v opačném případě . false Výchozí hodnota je false.
Příklady
Následující příklad ukazuje, jak pomocí AllowSorting vlastnosti povolit řazení v ovládacím GridView prvku při použití automaticky generovaných sloupců.
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView AllowSorting Example</title>
</head>
<body>
<form id="form1" 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="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView AllowSorting Example</title>
</head>
<body>
<form id="form1" 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>
Následující příklad ukazuje použití AllowSorting vlastnosti k povolení řazení v ovládacím GridView prvku při Columns definování kolekce. Obrázek se také programově přidá do záhlaví sloupce, který se seřadí, aby označil směr řazení. Aby tato ukázka fungovala, musíte zadat vlastní image.
<%@ 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_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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView AllowSorting Example</title>
</head>
<body>
<form id="form1" 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="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_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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView AllowSorting Example</title>
</head>
<body>
<form id="form1" 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>
Poznámky
Pokud je ovládací prvek zdroje dat, který podporuje řazení, vázán na GridView ovládací prvek, GridView může ovládací prvek využívat možnosti správy zdrojů dat a poskytovat funkce automatického řazení. GridView Pokud je ovládací prvek vázán na zdroj dat nastavením DataSource vlastnosti programově, je nutné zadat funkce řazení pomocí Sorting události.
Poznámka:
Různé zdroje dat mají různé požadavky na povolení jejich možností řazení. Pokud chcete určit požadavky, přečtěte si dokumentaci ke konkrétnímu zdroji dat.
Chcete-li povolit řazení, nastavte AllowSorting vlastnost na truehodnotu . Pokud je řazení povolené, text nadpisu pro každé pole sloupce se sadou SortExpression vlastností se zobrazí jako tlačítko odkazu.
Poznámka:
Vlastnost SortExpression automaticky vygenerovaného pole sloupců se vyplní automaticky. Pokud definujete vlastní sloupce v Columns kolekci, musíte nastavit SortExpression vlastnost pro každý sloupec. Jinak se v záhlaví nezobrazí tlačítko odkazu.
Kliknutím na tlačítko odkazu pro sloupec se položky v ovládacím GridView prvku seřadí podle výrazu řazení. Výraz řazení je obvykle jednoduše název pole zobrazeného ve sloupci, což způsobí GridView , že ovládací prvek seřadí s ohledem na tento sloupec. Pokud chcete řadit podle více polí, použijte výraz řazení, který obsahuje seznam názvů polí oddělených čárkami. Pomocí vlastnosti můžete určit výraz řazení, který GridView ovládací prvek používá SortExpression . Kliknutím na tlačítko odkazu sloupce opakovaně přepíná směr řazení mezi vzestupným a sestupným pořadím. Chcete-li určit aktuální směr řazení, použijte SortDirection vlastnost.
Ovládací GridView prvek poskytuje několik událostí, které můžete použít k provedení vlastní akce při řazení. V následující tabulce jsou uvedeny dostupné události.
| Zvláštní událost | Description |
|---|---|
| Sorted | Nastane, když je hypertextový odkaz pro řazení sloupce kliknut, ale po GridView ovládacím úchytu operace řazení. Tato událost se běžně používá k provedení úkolu poté, co uživatel klikne na hypertextový odkaz a seřadí sloupec. |
| Sorting | Nastane, když je hypertextový odkaz pro řazení sloupce kliknut, ale před GridView ovládacím úchytem operace řazení. Tato událost se často používá ke zrušení operace řazení nebo k provedení vlastní rutiny řazení. |