Solved

Sorting problem in Vb project for Numeric Data

Posted on 2006-11-09
15
334 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 142

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 142

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Program to display an alert on Windows Toolbar 2 77
This code tracks birthdays 3 73
Delphi: barcode reading on android platform 1 30
VB.net and sql server 4 35
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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