AspNet DataGrid won't fire sort routine

P

PatLaf

Hello to all,
I have an asp.net application that uses a datagrid but it
won't fire the sort command even though I've set the
allow sort to true. Any idea's?

Thanks in advance
Patrick Laferriere
 
M

Mike Moore [MSFT]

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:DataGrid 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:DataGrid>


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.


--------------------
 
P

PatLaf

I have the code posted that I'm trying to use. The only
real difference that I see is that you set auto gen col's
to false. Could that be a factor in why this isn't
working? I have code for the grid and the wire up in the
behind page. My issue is that the event never triggers.
It fires a page.postback but never the code to handle the
sort yet I have it there. I'm kind of confused as to why
this won't work. If the page post's back why doesn't it
fire the grid's sort command?

Regards,
Pat
-----Original Message-----
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:DataGrid 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:DataGrid>


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
 
A

Alvin Bruney

Your sort event handler is not wired up. Verify this in the events property
pages. It will be empty. There is a bug where it becomes unwired sometimes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top