Sorting works - sort of??

palehorse

Active member
Joined
Jan 28, 2005
Messages
34
Programming Experience
1-3
Hello everyone,

I have a datagrid that should be sorting two particular columns when the header is clicked. Well - one column is sorting fine - the other sorts fine up until the end and is out of order - but only when you get to the end of the list.

This is my code
VB.NET:
    Private Function CreateDataSource()
        Try
            Dim sSQL As String = "SELECT uid_pk,first_name,last_name FROM customers"
            Dim DA As SqlDataAdapter = New SqlDataAdapter(sSQL, CN)
            Dim DS As New DataSet
            DA.Fill(DS, "customers")
            Dim DV As New DataView(DS.Tables("customers"))
            DV.Sort = SortField
            CreateDataSource = DV
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Function
    Private Sub BindGrid()
        Try
            dg.DataSource = CreateDataSource()
            dg.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Private Sub dg_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dg.SortCommand
        Try
            SortField = e.SortExpression & " ASC"
            BindGrid()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub


Is this just an issue with the data in the db - or is my code wrong? Any suggestions would help. Let's just say the first_name field sorts everything fine (a-z) but towards the end (of about 200 names) lets say the last good sorted name is Zobo and after that it will show another name as Alexander when Alexander should be at the top - and after Alexander is Bobcat and then Charles - etc...so, it is in order 90% of the way - then starts back to the A's and goes in order again - does that make sense?
 
Ok.....I went and looked at the data in the db - seems some of these fields have leading spaces before the actual first name so the data looks like this:


Adam
Berny
Charles
Alexander
Bobby
Cheryl

So it is sorting correctly - the spaces are just throwing everything off...I tried to do a LTRIM(RTRIM(first_name)) but get a message saying something like:

No field or property was found named LTRIM(RTRIM(first_name))

I did this:

Select LTRIM(RTRIM(first_name)) first_name, last_name from customers

You can see I gave whatever results from LTRIM(RTRIM(first_name)) an actual name called first_name and it works!!! Still review what I have for sorting and let me know if it is efficient or not if you don't mind - thanks...
 
Back
Top