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")

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.

      Procedure Load
            Set Autoincerror Off
            Close Databases All
            Create Cursor junkdata (jdpk I Autoinc, prodfk I)
            For lnloop = 1 To 5
                  Append Blank
            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')

      Procedure grid1.Init
            With This
                  .RecordSourceType =1
                  .RecordSource = 'junkdata'
                  With .Column1
                      .Width = 200
                        .ControlSource = 'junkdata.prodfk'
                        .CurrentControl = 'Combo1'
                        .Combo1.Visible = .T.
                        .Sparse = .F.


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

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!

No comments:

Post a Comment