Solved

Sorting problem in Vb project for Numeric Data

Posted on 2006-11-09
15
341 Views
Last Modified: 2008-02-01
In my vb projet i have used combobox control and set its Style property to '2-DropDown' and sorted property as "True".

At the time of form load event i have filled this combo box with numeric fields. But in combo box drop down listing ,it treat that fields as string.

Current Output
E-G.       Database Table         In Combo-box Listing
             (record Set)
                   1                              1
                   2                              1
                   3                              10
                   7                              11
                   1                              2
                   9                              22
                   10                            3
                   22                            9


Expected Output Should be as follows:
E-G.       Database Table         In Combo-box Listing
             (record Set)
                   1                              1
                   2                              1
                   3                              2
                   7                              3
                   1                              7
                   9                              9
                   10                            10
                   22                            22

Thanks in Advance

0
Comment
Question by:badalpatel
  • 3
  • 2
  • 2
  • +4
15 Comments
 
LVL 3

Accepted Solution

by:
tang_tzuchi earned 125 total points
ID: 17912303
You just change your sql statement to sort int he order you want before populate to the drop down menu....
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17912332
... and change the "sorted" property to false on the combo box.

Indeed, combobox treats as strings.
hence, you could also left-pad the numerical value with spaces (best combined by applying a fixed-width font to the combobox)


0
 
LVL 6

Author Comment

by:badalpatel
ID: 17912393
My database is 'Access'. Actually I don't want to change the properties which i have set and i would like to sort the numbers in combobox in ascending order. They are populating in ascending order but same as string ordering instead of numerical order. Please...
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Assisted Solution

by:sivachirravuri
sivachirravuri earned 125 total points
ID: 17912568
To sort the records accoringly use the following query & fetch the record. This is one of the solutions that will be very useful.

select * from test order by cint(srno) asc

Sample VB code:

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Private Sub Form_Load()
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\VENEV6\Desktop\TEST.MDB;Persist Security Info=False"
rs.Open "select * from test order by cint(srno) asc", cn, adOpenDynamic, adLockOptimistic

While Not rs.EOF
    Combo1.AddItem rs(0)
    rs.MoveNext
Wend
End Sub


0
 
LVL 2

Expert Comment

by:mploschiavo
ID: 17917463
its sorting on ascii versus integer or decimal?..

why dont you changed the the datatype of the of the dataset.
0
 
LVL 6

Author Comment

by:badalpatel
ID: 17920028
hi sivachirravuri

i have used combobox control and set its Style property to '2-DropDown' and sorted property as "True".
i cant change this property due to my other requirement
0
 
LVL 5

Expert Comment

by:sivachirravuri
ID: 17927800
Hi Badalpatel,
you can use this code when combobox control's Style property is set to '2-DropDown'. But you need to make sored false. BCoz this code anywaz will do that.


0
 
LVL 12

Assisted Solution

by:sandip132
sandip132 earned 125 total points
ID: 17936268
In this example this is done for Listview, Please refer the code.

http://www.codeguru.com/vb/controls/vb_listview/article.php/c1669/
0
 
LVL 6

Author Comment

by:badalpatel
ID: 18290217
i didnt get my answer of my question..
anyway u can split it they all tried but there answer was not right
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18290435
>they all tried but there answer was not right
Well, I am sure that if you tried my suggestion with left-padding the numerical data with spaces it will work (I used that alot already)...

also, you never came back with the answer to if the field is numerical data type or not, I guess that table field is actually defined as text but contains numerical data, which is not the same as a numerical data type
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18542673
>i didnt get my answer of my question..
You did get answers, they just didn't tell you what you wanted to hear.

>anyway u can split it they all tried but there answer was not right

You can't change the comparison function for a drop-down list.  It ALWAYS sorts as text if Sorted = True.   No amount of expertise can change that.  If the drop-down list allowed you to define your own comparision function, then you would be able to sort by whatever you want.  Because it doesn't, you have to live with the sort order that it allows and format your data so it will sort in the order in which you would like to see it, or you have to sort it yourself before adding the list items in sorted order.  lBoth of these solutions have been offered to you, but you continue to ask for the impossible.  sandip132 comes the closest to what you want because the solution (s)he offers would appear to the user to be doing exactly what you've requested, but his solution requires you to use the Common Controls ListView with some significant C++ wrapper code.

You never explained your "other requirements" that forced you to set this box to Sorted = True.  That requirement is faulty and should be revisited; it is a techinical specification, not functional, and as such should be modified if the function you require (a list of options sorted according to your requirement) is to be implemented.

I recommend that Venabili's suggested split of points be made.

I suggest that Venabili's point distribution be accepted as the querent has rejected all correct solutions for this question.  
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question