Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASC / DESC Datagrid sorting.... 1

Status
Not open for further replies.

Isadore

Technical User
Feb 3, 2002
2,167
US
Wanted to introduce ASC/DESC sorting in a DataGrid using VB (found 10 or so C# routines on the net). The two I did find both failed.

The first routine, below, allows the 0 grid colum to sort both ascending and descending on consecutive cicks but only allows the remaining rows to sort ascending (code from 4GuysFromRolla.com). Any ideas on what may be going wrong?

Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
Dim SortExprs() As String
Dim CurrentSearchMode As String, NewSearchMode As String
Dim ColumnToSort As String, NewSortExpr as String

' Parse the sort expression - delimiter space
SortExprs = Split(e.SortExpression, " ")
ColumnToSort = SortExprs(0)

' If a sort order is specified get it, else default is descending
If SortExprs.Length() > 1 Then
CurrentSearchMode = SortExprs(1).ToUpper()
If CurrentSearchMode = "ASC" Then
NewSearchMode = "Desc"
Else
NewSearchMode = "Asc"
End If
Else ' If no mode specified, Default is descending
NewSearchMode = "ASC"
End If

' Derive the new sort expression.
NewSortExpr = ColumnToSort & " " & NewSearchMode

' Figure out the column index
Dim iIndex As Integer
Select Case ColumnToSort.toUpper()
case "AwwSiteCode"
iIndex = 0
case "Group_Name"
iIndex = 1
case "Waterbody_Name"
iIndex = 2
case "Description"
iIndex = 3
case "LastDate"
iIndex = 6
End Select

' alter the column's sort expression
dgGroups.Columns(iIndex).SortExpression = NewSortExpr

' Sort the data in new order
GetSites(NewSortExpr)

End Sub

...GetSites(sortField As String)is working properly.

**************

A second routine (FYI - lost carrying out the same functionaity (tested once):

Private Sub dgGroups_SortCommand(S As Object, e As DataGridSortCommandEventArgs)

Dim sOrder As String = dgGroups.Attributes("ORDER")
Dim sExp As String = dgGroups.Attributes("EXPRESSION")

If (sExp <> e.SortExpression) Then
sOrder = &quot;ASC&quot;
sExp = e.SortExpression
Else
sOrder = IIf(sOrder = &quot;ASC&quot;, &quot;DESC&quot;, &quot;ASC&quot;)
End If

Dim oDV As DataView 'failed here on 1st test
oDV.Sort = sExp & &quot; &quot; & sOrder

dgGroups.Attributes.Add(&quot;ORDER&quot;, sOrder)
dgGroups.Attributes.Add(&quot;EXPRESSION&quot;, sExp)
End Sub

....of if anyone out there has a VB routine that accomplishes the same. I suppose the former might be the best approach.
 
Here's my offering from a while back of a new class that inherits from datagrid, and offers autoreverse nearly for free.

Source & Example:

faq855-2026

:)
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Thanks Paul. This a very useful technique - I searched everywhere but the FAQs!
 
Paul: Nice FAQ; very academic for someone that wants to build a custom control. Looks like there are two fundamental approaches to sorting in a datagrid, via the DataView (as in your routine), or, alternatively, capturing the Column index in the Sort_Grid event and then hardcoding your sort fields. I adjusted somewhat the initial code I posted above and will post here just in case someone needs ref to it. Thanks for your help - its enjoyable watching you, Zarc, jFrost et al. do your thing - quite impressive indeed.

Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
Dim SortExprs() As String
Dim CurrentSearchMode As String, NewSearchMode As String
Dim ColumnToSort As String, NewSortExpr as String

'Parse the sort expression - delimiter space
SortExprs = Split(e.SortExpression, &quot; &quot;)
ColumnToSort = SortExprs(0)

'If a sort order is specified get it, else default is ascending
If SortExprs.Length() > 1 Then
CurrentSearchMode = SortExprs(1).ToUpper()
If CurrentSearchMode = &quot;ASC&quot; Then
NewSearchMode = &quot;Desc&quot;
Else
NewSearchMode = &quot;Asc&quot;
End If
Else 'If no mode specified, Default is descending
NewSearchMode = &quot;ASC&quot;
End If

'Derive the new sort expression.
NewSortExpr = ColumnToSort & &quot; &quot; & NewSearchMode

Dim iIndex As String
If ColumnToSort = &quot;AwwSiteCode&quot; Then
iIndex = 0
ElseIf ColumnToSort = &quot;Group_Name&quot; Then
iIndex = 1
ElseIf ColumnToSort = &quot;Waterbody_Name&quot; Then
iIndex = 2
ElseIf ColumnToSort = &quot;Description&quot; Then
iIndex = 3
ElseIf ColumnToSort = &quot;LastDate&quot; Then
iIndex = 7
ElseIf ColumnToSort = &quot;ChemCt&quot; Then
iIndex = 8
ElseIf ColumnToSort = &quot;BacCt&quot; Then
iIndex = 9
End If

lblA.Text = NewSortExpr 'show user sort order and field

'alter the column's sort expression
dgGroups.Columns(iIndex).SortExpression = NewSortExpr

'Sort the data in new order
GetSites(NewSortExpr) 'bind datagrid
End Sub

A working grid using this code can be seen at (use &quot;Cullman&quot; or &quot;Jefferson&quot; for a county):


(reads data from an Access database)

One other note. Keep in mind that if you are sending a page to Cache it will intercept the routine and bring back the original ordering.
 
Did you just not put the stationary header on this scrolling grid, or did you never quite get those widths to work properly?

I'm getting ready to try to maybe implement it in a project, and am just curious how hard I'm going to have to work with it. ;-)

paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Paul -

That's what I have been looking at two. You can take the approach we discussed earlier with the DataGrid inside a table -- and by using the following:
...
<asp:BoundColumn DataField=&quot;LastDate&quot; HeaderText=&quot;Last Date&quot; DataFormatString=&quot;{0:d}&quot; ItemStyle-width=&quot;40px&quot; Sortexpression=&quot;LastDate&quot;/>
...

I was able to keep the Grid widths behaving, so I did have some success with lining up the grid and the table columns - so you can take that approach. However, I want to be able to sort asc/desc also, so would like the header to be hyperlinked somehow.

Thought of a couple of things. In the first place if you had &quot;dueling&quot; grids, one showing the header and one showing the data (then columns would always behave) you could probably implement sort - but then you have to read the data twice so I abandoned that approach.

Another option is to nest the datagrid in a listbox or yet another datagrid. The young fella at Metabuilders has this project listed as one he wants to do -- and I've come across a few posts on the web where people have inquired.

To date I have not seen one. Again, the problem with the Table/Grid approach is make sure the total length of the Grid, table are such that you can control the internal grid width without a problem so it's doable.

Soon we'll have a solution to this. Perhaps if you could read only column headings into one Grid, and data into the other, and have then flip between posts you could create a very nice looking and well behaved (sorting) pair of grids.

I'll keep you posted Paul if I have any luck here - I will continue to work on this.

Thanks for all your help Paul. As I said above, the likes of you, Zarc, jfrost and others makes all of this dot NET academics much more pleasant and an enjoyable experience. There are alot of people on this forum that sincerely appreciate what you guys have done here.

Keep up the good work.
 
Isadore,

I normally don't specifically respond to direct thanks, but instead show my appreciation for such thanks by trying to go the extra mile to help that person on future problems (it's just my way), but you are always so overly thankful, that I felt compelled this time, so here goes:

<sincere>
It is people such as yourself (and many others in this forum and others) that keep me (and I'm sure others) coming back for more.

Helping people who:
(1)Are thankful to be helped --and--
(2 - and most important)Show significant increases in skill and ability along with a willingness to help others when they can, as you do and have

is just a very gratifying and enjoyable experience altogether. Besides, I learn alot from this forum and helping others, too, so I think we all gain from this experience.

So, I reciprocate your thanks, and thank you for making it fun.
</sincere>

See you around. [peace]

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top