Hi Patrick,
Gary is right that you need some code to make sorting work. Here is a
sample.
Add the following inside the HTML for the form. This adds a datagrid.
<asp
ataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION:
absolute; TOP: 8px" runat="server"
AutoGenerateColumns="False" AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="au_id" SortExpression="au_id"
HeaderText="au_id"></asp:BoundColumn>
<asp:BoundColumn DataField="au_fname" SortExpression="au_fname"
HeaderText="au_fname"></asp:BoundColumn>
<asp:BoundColumn DataField="au_lname" SortExpression="au_lname"
HeaderText="au_lname"></asp:BoundColumn>
</Columns>
</asp
ataGrid>
Next, add the following to your code behind.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
ViewState("SortOrder") = "DESC"
ViewState("SortField") = "au_id"
Bind()
End If
End Sub
Private Sub Bind()
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT au_id, au_fname, au_lname FROM
Authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
Dim sqlAdapter As New SqlClient.SqlDataAdapter(sqlCommand)
Dim ds As New Data.DataSet
sqlAdapter.Fill(ds, "data")
Dim dv As DataView
dv = ds.Tables("data").DefaultView
dv.Sort = ViewState("SortField") & " " & ViewState("SortOrder")
DataGrid1.DataSource = dv
DataGrid1.DataBind()
sqlAdapter.Dispose()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
If ViewState("SortField") = e.SortExpression Then
If (ViewState("SortOrder") = "DESC") Then
ViewState("SortOrder") = "ASC"
Else
ViewState("SortOrder") = "DESC"
End If
End If
ViewState("SortField") = e.SortExpression
Bind()
End Sub
Thank you, Mike
Microsoft, ASP.NET Support Professional
Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.
This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------