How to Sort ComboBox that has a DataSource?

Discussion in 'Microsoft ADO .NET' started by RamChip, Oct 29, 2003.

  1. RamChip

    RamChip Guest

    VB.net... MS Access data files

    Got 2 Comboboxes. Both are bound to a DataSet. One lists a groups of
    numbers. The other associated names.

    The current method is to list the Combobox with numbers (which they are in
    numeric order in the Access data file).

    What I want to accomplish is to give a user the method to toggle the
    associated names to be sorted or not (not at startup).

    When I try to Sort the Combobox of associated names (combobox2) I get the
    error:

    Can't sort a ComboBox that has a DataSource set. Sort the data using the
    underlying data model.

    How can this be accomplished?

    Many thanks

    Ralph
     
    RamChip, Oct 29, 2003
    #1
    1. Advertisements

  2. RamChip

    Paul Clement Guest

    On Wed, 29 Oct 2003 00:58:01 GMT, RamChip <> wrote:

    ¤ VB.net... MS Access data files
    ¤
    ¤ Got 2 Comboboxes. Both are bound to a DataSet. One lists a groups of
    ¤ numbers. The other associated names.
    ¤
    ¤ The current method is to list the Combobox with numbers (which they are in
    ¤ numeric order in the Access data file).
    ¤
    ¤ What I want to accomplish is to give a user the method to toggle the
    ¤ associated names to be sorted or not (not at startup).
    ¤
    ¤ When I try to Sort the Combobox of associated names (combobox2) I get the
    ¤ error:
    ¤
    ¤ Can't sort a ComboBox that has a DataSource set. Sort the data using the
    ¤ underlying data model.
    ¤
    ¤ How can this be accomplished?

    What it is saying is that you should perform your sort when you query the database so that your
    associated names are in order.


    Paul ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Oct 29, 2003
    #2
    1. Advertisements

  3. RamChip

    Mr. B Guest

    With Deft Fingers, Paul Clement <>
    wrote:

    >What it is saying is that you should perform your sort when you query the database so that your
    >associated names are in order.


    So in order for me to have both an option to view a sorted number Combobox and
    a names combobox I'll need to have 2 datasets? Oh well... okay. Kinda knew I
    could do that... but hoped that there was a way to do it via code.

    Thanks!
     
    Mr. B, Oct 29, 2003
    #3
  4. RamChip

    Paul Clement Guest

    On Wed, 29 Oct 2003 15:33:47 GMT, Mr. B <> wrote:

    ¤ With Deft Fingers, Paul Clement <>
    ¤ wrote:
    ¤
    ¤ >What it is saying is that you should perform your sort when you query the database so that your
    ¤ >associated names are in order.
    ¤
    ¤ So in order for me to have both an option to view a sorted number Combobox and
    ¤ a names combobox I'll need to have 2 datasets? Oh well... okay. Kinda knew I
    ¤ could do that... but hoped that there was a way to do it via code.

    The only other way I can think of is to use an Array as the DataSource which would of course require
    the additional step of getting your data into the array.


    Paul ~~~
    Microsoft MVP (Visual Basic)
     
    Paul Clement, Oct 29, 2003
    #4
  5. Actually you could accomplish this if you create 2 DataView objects from the DataTable and sort each one as you like then bind each DataView object to the ComboBox
    desired. The following demonstrates how to do it:

    Dim da1 As SqlDataAdapter = New SqlDataAdapter("select * from Customers", cn)
    da1.Fill(ds, "customers")

    Dim dv1 As DataView = New DataView(ds.Tables("Customers"))
    Dim dv2 As DataView = New DataView(ds.Tables("Customers"))

    dv1.Sort = "CustomerID"
    ComboBox1.DataSource = dv1
    ComboBox1.DisplayMember = "CustomerID"

    dv2.Sort = "CompanyName"
    ComboBox2.DataSource = dv2
    ComboBox2.DisplayMember = "CompanyName"

    I hope this helps!


    Thanks,
    Hussein Abuthuraya
    Microsoft Developer Support

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
    http://www.microsoft.com/security.
     
    Hussein Abuthuraya[MSFT], Oct 30, 2003
    #5
  6. RamChip

    Mr. B Guest

    On Thu, 30 Oct 2003 00:49:09 GMT, in microsoft.public.dotnet.framework.adonet
    you wrote:

    >Actually you could accomplish this if you create 2 DataView objects from the DataTable and sort each one as you like then bind each DataView object to the ComboBox
    >desired. The following demonstrates how to do it:


    >I hope this helps!


    Thanks. Yes it does. Actually I was (eventually) going to just create
    another DataAdapter (for sorting via names). Then toggle between my original
    DataAdapter (which is the default sort by numbers) and the 2nd DA.

    But I'll try your method (always looking for better coding methods).

    Thanks!
     
    Mr. B, Oct 30, 2003
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paulo Henrique Castro

    combobox datasource framework 1.1

    Paulo Henrique Castro, Jul 8, 2003, in forum: Microsoft ADO .NET
    Replies:
    0
    Views:
    590
    Paulo Henrique Castro
    Jul 8, 2003
  2. Guest
    Replies:
    1
    Views:
    305
    Guest
    May 27, 2005
  3. DraguVaso

    adding extra items on top of ComboBox DataSource

    DraguVaso, Oct 4, 2005, in forum: Microsoft ADO .NET
    Replies:
    6
    Views:
    1,412
    DraguVaso
    Oct 5, 2005
  4. Guest
    Replies:
    4
    Views:
    460
    Guest
    Jan 17, 2006
  5. Shawn Repphan

    ComboBox datasource problem

    Shawn Repphan, Mar 28, 2006, in forum: Microsoft ADO .NET
    Replies:
    0
    Views:
    236
    Shawn Repphan
    Mar 28, 2006
Loading...

Share This Page