i have a dataview contains a column age (datatype as string)....with the data like "35/5" (i.e 35 years and 5 months) ,"9/6","101/4","23/3".....

when i am trying to sort the gridview by age asc..it diplays the result as 101/4,"23/3","35/5","9/6"
it is sort with the first char of each data...but i need the result as "9/6","23/3","35/5",101/4 like this..pls give some guide lines...

it seems better to sort data before to send it to GridView. Can you give a code sample where you are reading data form some source and putting to GridView (all the steps from SQL (XML, etc) to GridView) ?

When you are sorting data in GridView, then sorted is only current page, but not all scope of data.

Dim dv As Data.DataView = MakeData()
dv.Sort = "Age Asc"
GridView1.DataSource = dv
GridView1.DataBind()

Private Function MakeData() As Data.DataView
Dim dv As New Data.DataView
Dim dt As New System.Data.DataTable
Dim dc1 As New Data.DataColumn("Name", GetType(String))
Dim dc2 As New Data.DataColumn("Id", GetType(Integer))
Dim dc3 As New Data.DataColumn("Desc", GetType(String))
Dim dc4 As New Data.DataColumn("Con", GetType(String))
Dim dc5 As New Data.DataColumn("Age", GetType(String))
Dim strSpace As String = vbTab & vbTab & vbTab & vbTab
dt.Columns.Add(dc1)
dt.Columns.Add(dc2)
dt.Columns.Add(dc3)
dt.Columns.Add(dc4)
dt.Columns.Add(dc5)
Dim dr As Data.DataRow
dr = dt.NewRow
dr(dc1) = "Vimal"
dr(dc2) = 1
dr(dc3) = "Vincent"
dr(dc4) = dr(dc1) & strSpace & dr(dc3)
dr(dc5) = "56/5"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(dc1) = "Srini"
dr(dc2) = 2
dr(dc3) = "Ramasamy"
dr(dc4) = dr(dc1) & strSpace & dr(dc3)
dr(dc5) = "12/7"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(dc1) = "Amar"
dr(dc2) = 3
dr(dc3) = "Sudarson"
dr(dc4) = dr(dc1) & strSpace & dr(dc3)
dr(dc5) = "101/4"
dt.Rows.Add(dr)
dr = dt.NewRow
dr(dc1) = "Rajan"
dr(dc2) = 4
dr(dc3) = "Esakki"
dr(dc4) = dr(dc1) & strSpace & dr(dc3)
dr(dc5) = "9/5"
dt.Rows.Add(dr)
dv.Table = dt
Return dv
End Function

Then it seems better to create separate Int columns for months and years, and also a countable column

Dim dsCountable As New Data.DataColumn("TotalMonths", GetType(Int32), "Years * 12 + Months") - third parameter is expression to count value, and sort by this column

dv.Sort = "TotalMonths Asc"

my application need like this requirement so i workout one test applns..i am giving the piece of coding from that test applns ..actually in my appln the data comes from oracle db and the age columns having null value also

Then the best way is to sort data in DB, in SQL-request. I do so in MS SQL, and I think there is such a possibility in Oracle too.

If you do not have access to DB, then the solution might be to create an ArrayList of items (you create some class, create an instance for each DB row, fill properties with data, and sort array list).

ArrayList is easily sorted (I have an example in C#)

arrList.Sort(new SortItems());

class SortItems : IComparer
    {

        public SortItems()
        {
        }

        // Implemnentation of the IComparer:Compare 
        // method for comparing two objects.
        public int Compare(object x, object y)
        {
            YourItem xItem = (YourItem)x;
            YourItem yItem = (YourItem)y;
            // here you compare ages and return 1 if x's age is larger than y's,
  // 0 if they are equal
  // and -1 if y's age is larger than x's
            return 0;
        }

    }

And then you fill your items from ArrayList

Edited 3 Years Ago by pyTony: fixed formatting

Thanks for ur very useful reply....i ll proceed with this..

This article has been dead for over six months. Start a new discussion instead.