Results 1 to 11 of 11

Thread: [RESOLVED] How to Sort Data Bound Combobox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    26

    Resolved [RESOLVED] How to Sort Data Bound Combobox

    Hi Guys!,

    i am having this issue "ComboBox that has a DataSource set cannot be sorted. Sort the data using the underlying data model." is there a way to sort out data bound items?

    i have been researching as well and found this on google

    SiteBindingSource.DataSource = myDataTable <---- i can't find how to do myDataTable
    SiteBindingSource.Sort = "SomeColumn" <----- changed this to "[Site Name]"
    Combobox1.DataSource = SiteBindingSource

    but i can't seem to make this work. i hope that somebody can help me on this

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Sort Data Bound Combobox

    Show us the code that you currently have to populate your ComboBox.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    26

    Re: How to Sort Data Bound Combobox

    there's no codes to populate my combobox. i directly used the combobox properties and bind the items.

    Update: i have found a work around but there will be a lot of work for me. i will have to change my column format directly from database because i am currently using "TEXT" and i need to change it to "VARCHAR(MAX)" for me to be able to sort it using properties from the combobox.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: How to Sort Data Bound Combobox

    If your using a BindingSource as the Datasource for the Combobox then all you have to do is sort the bindingsource. In the form Load event yourBindingsource.Sort = "itemsFieldName"

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to Sort Data Bound Combobox

    It looks like that's what he was trying to do here:
    SiteBindingSource.Sort = "SomeColumn"

    But, if the type was Text... it may not have been sorting right since text is a "special" type and isn't treated the same as string (varchar).


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: How to Sort Data Bound Combobox

    Quote Originally Posted by techgnome View Post
    It looks like that's what he was trying to do here:
    SiteBindingSource.Sort = "SomeColumn"

    But, if the type was Text... it may not have been sorting right since text is a "special" type and isn't treated the same as string (varchar).


    -tg
    Never used a Text data type field in Sql Server, Text was the string data type in Access. What's so special about the Text data type in Sql Server which makes it sort differently?

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to Sort Data Bound Combobox

    It's a form of BLOB. In some circles it would be called a CLOB.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: How to Sort Data Bound Combobox

    Sounds like it's not the best data type to use. Read this in MS Docs,

    IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Sort Data Bound Combobox

    Quote Originally Posted by wes4dbt View Post
    Sounds like it's not the best data type to use. Read this in MS Docs,
    Those data types have been obsolete for at least a decade now - probably significantly more. Anyone who is using them for a new database is making a bad decision and almost certainly hasn't done enough reading.
    Quote Originally Posted by ahmish View Post
    i have found a work around but there will be a lot of work for me. i will have to change my column format directly from database because i am currently using "TEXT" and i need to change it to "VARCHAR(MAX)" for me to be able to sort it using properties from the combobox.
    That's not really a workaround. Your issue only exists because you used the wrong data type in the first place so this is the correct way to make the problem go away so no solution is required.

    It concerns me that you're considering using varchar(max) though. If you are displaying these values in a ComboBox and sorting them, exactly how long do you expect them to be? Even 100 characters would be lot to display in a ComboBox. There's no way that a database column intended to contain values for display in a drop-down list needs to be varchar(max). Decide on a sensible maximum length for those values and specify that in the column definition. Start thinking a bit about your database schema.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    26

    Re: How to Sort Data Bound Combobox

    Quote Originally Posted by jmcilhinney View Post
    Those data types have been obsolete for at least a decade now - probably significantly more. Anyone who is using them for a new database is making a bad decision and almost certainly hasn't done enough reading.

    That's not really a workaround. Your issue only exists because you used the wrong data type in the first place so this is the correct way to make the problem go away so no solution is required.

    It concerns me that you're considering using varchar(max) though. If you are displaying these values in a ComboBox and sorting them, exactly how long do you expect them to be? Even 100 characters would be lot to display in a ComboBox. There's no way that a database column intended to contain values for display in a drop-down list needs to be varchar(max). Decide on a sensible maximum length for those values and specify that in the column definition. Start thinking a bit about your database schema.
    thanks for the advise. i am new in coding and sql database with zero to maybe 5% knowledge. i am learning coding and sql database thru google only and learned for the last 3 months. i have a database columns which are mostly set as "TEXT" which i know now that is obsolete. i am now planning to migrate my database columns from "TEXT" to "Varchar" my concern here is i have a lot of columns with different functionality. i am sure that i need to use "varchar(max)" for atleast 2 columns. then i have columns that has minimum 50 characters and up to unknown characters depending on the site name. i will be checking the different types of tables.

    is it safe to say that

    INT - will be used for numbers
    Varchar - for text

    what about?

    Nchar?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to Sort Data Bound Combobox

    Quote Originally Posted by ahmish View Post
    what about?

    Nchar?
    You don't need to ask us questions like that. You just need to read the relevant documentation and decide which description matches the type of data that you want to store. If you don't know what data type to use then you either don't understand the data types - in which case you should be reading up on the data types - or you don't understand your data.

    char: fixed-length, single-byte
    nchar: fixed-length, multi-byte
    varchar: variable-length, single-byte
    nvarchar: variable-length, multi-byte

    You only have to ask yourself two question about your text data and the answers determine which data type to use. Although you can, it is rare that you would use a fixed-length data type for variable-length data because any value that is shorter will be padded with spaces and it's then up to you to remove them every time you retrieve the data. I generally only use fixed-length data types when the data is genuinely fixed-length, e.g. codes for US states. I think that fixed-length may actually be more efficient even if the data isn't always the same size, as long as the variation as small.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width