Ordinare le righe di un Datagrid in base ad una colonna (crescente o decrescente)
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<script language="VB" runat="server">
Dim MyConnection As OleDbConnection
Public Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("database/utenti.mdb"))
If Not (IsPostBack) Then
BindGrid("id")
End If
End Sub
Sub MyDataGrid_Sort(ByVal Sender As Object, ByVal E As DataGridSortCommandEventArgs)
BindGrid(E.SortExpression)
End Sub
Sub BindGrid(ByVal SortField As String)
Dim DS As DataSet
Dim MyCommand As OleDbDataAdapter
MyCommand = New OleDbDataAdapter("select * from utenti", MyConnection)
DS = New DataSet
MyCommand.Fill(DS, "utenti")
Dim Source As DataView = DS.Tables("utenti").DefaultView
Source.Sort = SortField
MyDataGrid.DataSource = Source
MyDataGrid.DataBind()
End Sub
</script>
<body>
<form runat="server" ID="Form1">
<ASP:DataGrid id="MyDataGrid" runat="server" OnSortCommand="MyDataGrid_Sort" Width="700px" BackColor="White"
BorderColor="#CC9966" CellPadding="4" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd"
AllowSorting="True" BorderStyle="None" BorderWidth="1px" Font-Names="Verdana">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</ASP:DataGrid>
</form>
</body>
</HTML>