Tuesday, May 21, 2013

ComboBox in a Grid - Combobox Trick # 2


I have promised last time I will show another combobox trick but totally forgot about it.  Then yesterday another member asked about a combobox inside a grid saying while it is working on his end, whatever is selected on the active combobox does not appear.  It appears only when the combobox on the current record looses focus.

While working directly with grids do not appeal to me, I played with his requirement and indeed it does not show the combobox' DisplayValue.  So I got curious because if I recall properly, I fixed that problem on my end before but in the end working directly on a Grid does not really appeal to me that I discarded the entire concept and went back to Add To Table approach.

Anyway, here is a working sample of that if you need one, and on the bottom note the reason why:




loTest = Createobject("Sample")
loTest.Show(1)

Define Class Sample As Form
      Caption = 'Combobox in a Grid'
      AutoCenter = .T.
      Width = 240

      Add Object grid1 As Grid With ;
            ColumnCount = 1,;
            RowHeight = 22,;
            Width = 220,;
            DeleteMark = .F.

      Add Object command1 As CommandButton With ;
            top = 220, Caption = '\<Browse'

      Procedure Load
            Set Autoincerror Off
            Close Databases All
            Create Cursor junkdata (prodfk I)
            For lnloop = 1 To 5
                  Append Blank
            Next
            Go Top

            Create Cursor junkproduct (prodpk I Autoinc, products c(30))
            Insert Into junkproduct Values (0,'Coke')
            Insert Into junkproduct Values (0,'Pepsi')
            Insert Into junkproduct Values (0,'Sprite')
            Insert Into junkproduct Values (0,'Mirinda')
            Insert Into junkproduct Values (0,'Royal')
            Insert Into junkproduct Values (0,'7-Up')
      Endproc

      Procedure grid1.Init
            With This
                  .RecordSourceType =1
                  .RecordSource = 'junkdata'
                  With .Column1
                        .Width = 200
                        .ControlSource = 'junkdata.prodfk'
                        .AddObject('Combo1','MyCombo')
                        .CurrentControl = 'Combo1'
                        .Combo1.Visible = .T.
                        .Sparse = .F.
                  Endwith
            Endwith
      Endproc

      Procedure command1.Click
            Select junkdata
            Browse Normal
      Endproc

Enddefine

Define Class MyCombo As ComboBox
      RowSourceType = 2
      BoundColumn = 2
      BoundTo = .F.
      RowSource = 'junkproduct.products,prodpk'
      Value = 0
      Style = 2
      ControlSource = 'junkdata.prodfk'
Enddefine




See that it is showing the proper DisplayValue on the active combobox immediately after you selected on its dropdown portion?  Well the problem, AFAIK, is about the .BoundTo property plus the underlying field type.  Since the underlying field type (ControlSource) is of integer type while the DisplayValue of the Combobox is of Character type, those clashes.  And the solution to that is to set BoundTo property to .F. 

Study the sample and Enjoy!


4 comments:

  1. Hello. I'm a bit late to this party but after reading this post, I thought that I'd found a solution to the issue I was having with comboboxes in a grid. Unfortunately, there is still a glitch that I'm seeing with this code; if you run the form and set some values via the dropdown list then close the form, on running it again, the selected values are not displayed (even though the underlying table has them saved). Do you know what is causing that to happen?

    ReplyDelete
  2. Oops. Nothing wrong with your code example. I just tried it again and everything is working as desired. I apologize for the mistake.

    ReplyDelete
  3. Hi, thanks for the confirmation.

    ReplyDelete