StrataFrame Forum

Change Combobox column order at runtime

http://forum.strataframe.net/Topic24920.aspx

By Aaron Young - 10/14/2009

Hi,



Is it possible to switch around the column order in a multi-column combobox at runtime? For example, I have a customer dropdown with two columns (accno and name). I normally show the accno on the left before the name. However, some users would like to see it the other way around so I would like to be able to do that at run time.



Can the columns be switched around at runtime?



Thanks,



Aaron
By Greg McGuffey - 10/15/2009

You could just have code to reset the appropriate property (not sure what its called) at runtime. Take a look in the designer file in the InitializeComponent method and you'll see it.
By Aaron Young - 10/15/2009

Thanks Greg,



I should have included in my post but I originally tried changing the PopulationDataSourceSettings but it doesn't change the order. It looks like the combobox has to be reset in some way after the changes have been made and that is what I am missing.



Aaron
By Greg McGuffey - 10/15/2009

Aaron,



You need to do the following:



Based on the desired configuration of the drop down:



1. Clear the Columns collection of the combo (always do this)

2. create desired columns with properties set

3. Add those columns to combo

4. Change the DropDownFormattingString property to match columns.



Here is some sample code:



Private Sub SetDropDownStyle(ByVal style As String)

  '-- Establish locals for our two columns. There is nothing to keep

  '   us from changing how many columns are are shown in the

  '   drop down, but we'll use two for both cases in this example.

  Dim MultiColumnItem1 As MicroFour.StrataFrame.UI.Windows.Forms.MultiColumnItem = New MicroFour.StrataFrame.UI.Windows.Forms.MultiColumnItem

  Dim MultiColumnItem2 As MicroFour.StrataFrame.UI.Windows.Forms.MultiColumnItem = New MicroFour.StrataFrame.UI.Windows.Forms.MultiColumnItem

  Dim ddFormatString As String = String.Empty



  '-- Based on some predetermined style, setup combo.

  Select Case style

    Case "P"

      '-- Show a phone number first, then the name. Name column is set wider.

      MultiColumnItem1.Name = "Phone"

      MultiColumnItem2.Name = "Name"

      MultiColumnItem2.Width = 120



      '-- Setup format string. These refer to display members setup in the

      '   population list settings of the combo. {0} is first name,

      '   {1} is last name and {2} is phone number.

      ddFormatString = "{2}|{0} {1}"



    Case "N"

      '-- Show the name first, then the phone number. Name column is set wider.

      MultiColumnItem1.Name = "Name"

      MultiColumnItem1.Width = 120

      MultiColumnItem2.Name = "Phone"



      '-- Setup format string.

      ddFormatString = "{0} {1}|{2}"

  End Select



  '-- Be sure to clear the existing columns so there are no conflicts with the new columns.

  Me.cboCustomers.Columns.Clear()



  '-- Add the new columns and set format string.

  Me.cboCustomers.Columns.AddRange(New MicroFour.StrataFrame.UI.Windows.Forms.MultiColumnItem() {MultiColumnItem1, MultiColumnItem2})

  Me.cboCustomers.PopulationDataSourceSettings.DropDownFormatString = ddFormatString

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  '-- Setup combo with "Phone Name" style for drop down.

  Me.SetDropDownStyle("P")



  '-- You need to requery for the settings to take effect. (settings define how the

  '   combo is filled, so they do nothing until it is refilled)

  Me.cboCustomers.Requery()

End Sub



By Aaron Young - 10/15/2009

Thanks Greg. I will give this a try.

I really appreciate your time and help on this one Smile

Thanks again

By Greg McGuffey - 10/15/2009

Any time. Let us know if you get it working.
By Aaron Young - 10/18/2009

Thanks Greg, works a treat! Smile



Many thanks!
By Greg McGuffey - 10/19/2009

Excellent! Glad you go it going.