locked
how to sort data in combo box RRS feed

  • Question

  • hi

    i have a combo box that shows a list of persons. combo has a data bindings property:
    bsPerson.Person

    (Me.cboIDperson.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.bsPerson, "IDperson", True)))


    if i try to set combo's property sorted from false to true i get a message: property value is not valid; ComboBox that has a DataSource set cannot be sorted. Sort the data using the underlying data model.

    i tried to do this:

    Dim dv As DataView
    dv = Me.ds.Person.DefaultView
    dv.Sort = "LastNameFirstName"

    Me.cboIDperson.DataSource = dv
    Me.cboIDperson.DisplayMember = "LastNameFirstName"
    Me.cboIDperson.ValueMember = "IDperson"

    but when i open form first data displayed in combo is not the one that should be. first data displayed is first in sorted list and not the one that is in database. when i go to next record then the data is adequate to the one in database, but every time i move to the first record the data displayed in combo is not the same as the one in database.

    so, please, how can i solve this problem ?

    thanks in advance
    Monday, June 25, 2007 12:26 PM

Answers

  • If I understand correctly, the code you posted above using the DataView works, it just does not default to the desired value?

     

    Where is the code that binding the remainder of the form?

     

    Try binding the combo box *before* you bind the rest of the form so that when you bind the IDperson field, the combo box is already populated.

     

    Hope this helps.

    Monday, June 25, 2007 7:23 PM
  • I don't use table adapters, so don't have specific details... but see if you can modify the code in the load method to fill the table for the combo box before you fill the table for the main form data...
    Monday, June 25, 2007 9:43 PM

All replies

  • If I understand correctly, the code you posted above using the DataView works, it just does not default to the desired value?

     

    Where is the code that binding the remainder of the form?

     

    Try binding the combo box *before* you bind the rest of the form so that when you bind the IDperson field, the combo box is already populated.

     

    Hope this helps.

    Monday, June 25, 2007 7:23 PM
  • combo works with all other records except for the first one.

    is there any other way to do it?

    how can i bind combo "before" the rest of the form? i mean, all fields on the form are binded by the same binding source object - bsCourt. all the code is in InitializeComponent method of the form, except for the table adapters that fill tables which are in load method.

    all i want to do is to get persons sorted in alphabetic order in combo.
    • Proposed as answer by lipsa das Thursday, September 18, 2008 8:25 AM
    Monday, June 25, 2007 8:00 PM
  • I don't use table adapters, so don't have specific details... but see if you can modify the code in the load method to fill the table for the combo box before you fill the table for the main form data...
    Monday, June 25, 2007 9:43 PM
  • You could open the dataset designer and add an order by clause on the tableadapter that the combobox is bound to.

    so if your combobox was bound to combobox tableadapter then you would doubleclick on the dataset in your solution to open the designer then rightclick on the tableadapter that the combobox is bound too and the click configure and just add and order by clause to the end of the code that comes up.

    • Proposed as answer by gc-lopes Thursday, May 26, 2011 2:53 PM
    Saturday, August 25, 2007 1:37 AM