datagrid bidirectional sorting

M

mark

i have populated a datagrid succesfully and enabled sort successfully except
its not bidirectional - how do i do this ?

this is my code for sorting

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim strConn As String = connectionstring
Dim conn As New System.Data.SqlClient.SqlConnection(strConn)
Dim sql As String = "SELECT * FROM extensionlist ORDER BY " &
e.SortExpression
Dim da As New System.Data.sqlclient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
conn.Close()
Label5.Text = e.SortExpression
End Sub

i know its something to do with adding DESC and ASC to the sql string but im
not sure how

thanks

mark
 
A

avnrao

you are correct. all you have to do is..
keep a session variable holding asc or desc and next time when clicked,
toggle between asc and desc.

its Order by columnname asc/desc

Av.
 
S

S. Justin Gengo

Mark,

'Create a sort variable.

Dim Sort As String

'Check if sort is in viewstate
If Not ViewState.Item("Sort") Is Nothing Then
Sort = ViewState.Item("Sort")
'Toggle the sort
If Sort = "ASC" Then
Sort = "DESC"
Else
Sort = "ASC"
End If
Else
'First sort is ascending
Sort = "ASC"
End If

'Then tack it on to the select.

Dim sql As String = "SELECT * FROM extensionlist ORDER BY" &
e.SortExpression & " " & Sort

'Store the current sort in viewstate (or elsewhere for postback)
ViewState.Add("Sort", Sort)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
A

Ashish M Bhonkiya

Hi Mark,

you may like to read this article

http://www.dotnetjunkies.com/Article/3061D828-4D4E-4381-BE9F-02589F55C72C..dcik

Regards
Ashish M Bhonkiya

i have populated a datagrid succesfully and enabled sort successfully except
its not bidirectional - how do i do this ?

this is my code for sorting

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim strConn As String = connectionstring
Dim conn As New System.Data.SqlClient.SqlConnection(strConn)
Dim sql As String = "SELECT * FROM extensionlist ORDER BY " &
e.SortExpression
Dim da As New System.Data.sqlclient.SqlDataAdapter(sql, conn)
Dim ds As New DataSet
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
conn.Close()
Label5.Text = e.SortExpression
End Sub

i know its something to do with adding DESC and ASC to the sql string but im
not sure how

thanks

mark
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top