locked
How do I put a Combobox in a Grid? RRS feed

  • Question

  • How do I put a Combobox in a Grid?
    Wednesday, February 24, 2010 5:42 PM

Answers

  • Right click on your grid and select "Edit", then click on the desired column and select the default control (tipically a textbox) in the properties window.  Then press "Delete" to get rid of the default control (tipically a textbox).   

    Then click on the Combobox icon in your form controls toolbar and click in the desired column.  Yo can now set all the properties and put code in your combobox.
    • Marked as answer by Martin_Xie Wednesday, March 3, 2010 8:40 AM
    Wednesday, February 24, 2010 6:15 PM
  • It is best done in code. You can do that visually but would be hard. If you would do visually then:

    -Put a combobox control on form and set its properties and whatever methods you want there (it is cumbersome to do if it is in the column, do on the form)
    -When done, select it and cut with Ctrl+X
    -Put the grid in edit mode by Ctrl+Click and then click the column that you'd put that into
    -Paste using Ctrl+V
    -Set Column's CurrentControl property to combo's name
    -Set column's sparse to .F. if that is really what you want to do 

    Here is a sample in code:

    ** Author:Cetin Basoz
    ** Combo in grid column sample
    
    Public oForm
    oForm = Createobject('comboingrid')
    oForm.Show
    
    Define Class comboingrid As Form
      Top = 0
      Left = 0
      Height = 350
      Width = 620
      DataSession=2
    
      Add Object grdorditems As Grid With ;
        Height = 300, ;
        Left = 10, ;
        Top = 10, ;
        Width = 600, ;
        Rowheight = 19,;
        RecordSource = "orditems"
    
      Procedure Load
        Use _samples+'Data\products' In 0
        Use _samples+'Data\orditems' In 0 Order Tag order_id
        Set Multilocks On
        CursorSetProp("Buffering",5,'orditems')
      Endproc
    
      Procedure grdorditems.Init
        Local ix
        With This
          For ix = 1 To .ColumnCount
            If Upper(Justext(.Columns(m.ix).ControlSource)) == 'PRODUCT_ID'
              With .Columns(m.ix)
                .Bound = .F.
                .ControlSource = "(Iif(Seek(orditems.product_id,"+;
                  "'products','product_id'),Products.prod_name,''))"
                .Width = 170
                .AddObject('comboincol','combobox')
                With .ComboIncol
                  .BoundColumn = 2
                  .BoundTo = .T.
                  .ColumnCount = 2
                  .ColumnWidths = "180,0"
                  .RowSourceType = 3
                  .RowSource = "select products.prod_name,product_id"+;
                    " from products into cursor crsProducts order by 1"
                  .SpecialEffect = 1
                  .Style = 2
                  .ControlSource = "orditems.product_id"
                  .Visible = .T.
                Endwith
                .CurrentControl = 'comboincol'
              Endwith
              Exit
            Endif
          Endfor
        Endwith
      Endproc
    Enddefine
    

    • Marked as answer by Martin_Xie Wednesday, March 3, 2010 8:41 AM
    Wednesday, February 24, 2010 8:28 PM

All replies

  • Right click on your grid and select "Edit", then click on the desired column and select the default control (tipically a textbox) in the properties window.  Then press "Delete" to get rid of the default control (tipically a textbox).   

    Then click on the Combobox icon in your form controls toolbar and click in the desired column.  Yo can now set all the properties and put code in your combobox.
    • Marked as answer by Martin_Xie Wednesday, March 3, 2010 8:40 AM
    Wednesday, February 24, 2010 6:15 PM
  • It is best done in code. You can do that visually but would be hard. If you would do visually then:

    -Put a combobox control on form and set its properties and whatever methods you want there (it is cumbersome to do if it is in the column, do on the form)
    -When done, select it and cut with Ctrl+X
    -Put the grid in edit mode by Ctrl+Click and then click the column that you'd put that into
    -Paste using Ctrl+V
    -Set Column's CurrentControl property to combo's name
    -Set column's sparse to .F. if that is really what you want to do 

    Here is a sample in code:

    ** Author:Cetin Basoz
    ** Combo in grid column sample
    
    Public oForm
    oForm = Createobject('comboingrid')
    oForm.Show
    
    Define Class comboingrid As Form
      Top = 0
      Left = 0
      Height = 350
      Width = 620
      DataSession=2
    
      Add Object grdorditems As Grid With ;
        Height = 300, ;
        Left = 10, ;
        Top = 10, ;
        Width = 600, ;
        Rowheight = 19,;
        RecordSource = "orditems"
    
      Procedure Load
        Use _samples+'Data\products' In 0
        Use _samples+'Data\orditems' In 0 Order Tag order_id
        Set Multilocks On
        CursorSetProp("Buffering",5,'orditems')
      Endproc
    
      Procedure grdorditems.Init
        Local ix
        With This
          For ix = 1 To .ColumnCount
            If Upper(Justext(.Columns(m.ix).ControlSource)) == 'PRODUCT_ID'
              With .Columns(m.ix)
                .Bound = .F.
                .ControlSource = "(Iif(Seek(orditems.product_id,"+;
                  "'products','product_id'),Products.prod_name,''))"
                .Width = 170
                .AddObject('comboincol','combobox')
                With .ComboIncol
                  .BoundColumn = 2
                  .BoundTo = .T.
                  .ColumnCount = 2
                  .ColumnWidths = "180,0"
                  .RowSourceType = 3
                  .RowSource = "select products.prod_name,product_id"+;
                    " from products into cursor crsProducts order by 1"
                  .SpecialEffect = 1
                  .Style = 2
                  .ControlSource = "orditems.product_id"
                  .Visible = .T.
                Endwith
                .CurrentControl = 'comboincol'
              Endwith
              Exit
            Endif
          Endfor
        Endwith
      Endproc
    Enddefine
    

    • Marked as answer by Martin_Xie Wednesday, March 3, 2010 8:41 AM
    Wednesday, February 24, 2010 8:28 PM