Solved

Sorting problem in Vb project for Numeric Data

Posted on 2006-11-09
15
326 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wefewf 2 45
How to split this in C++ 4 78
STDEVP in SQL 2 38
Hashing Algorithm 5 32
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now