How to add my own objects to a grid programatically

Hi all;
I'm using this method to construct the columns within the grid. I need however to construct the grid so the cells are my own class objects.

WITH thisform.kgrdledger1
.columnCount = -1
.RecordSource = "CurRpt"
.addcolumn(1)
.columns(thisform.kgrdledger1.ColumnCount).name = "colAmount"
.colAmount.controlsource = "curRpt.Amount"
.colAmount.header1.caption = "$ Amount"
*****===>>> Here I would like to add object class 'mytxt1' instead of text1
endwith


Thank you all.


Answer this question

How to add my own objects to a grid programatically

  • opera100

    Thank you all... However:

    When I try to set the controlsource for the new object I get an error telling me that the parent object wont allow that.
    I tried to set the bound property to .F. but then the column is blank (No data shown).
    I also tried to clear out the column's control source but still it won't allow me to add a control source to my object.
    Must be something stupid I didn't know... And ideas

    WITH thisform.kgrdledger1
    .columnCount = -1
    .RecordSource = "CurRpt"
    .addcolumn(1)
    .columns(thisform.kgrdledger1.ColumnCount).name = "colMainFld"
    WITH .colMainFld
    .Addobject( 'txtMainFld', 'mytextbox' )
    .txtMainFld.visible = .T.
    * .bound = .F.
    * .sparse = .T.
    * .controlsource = "curRpt.mainfld"
    .txtMainFld.controlsource = "curRpt.mainfld"
    .header1.caption = thisform.cMainFldCaption
    .RemoveObject( 'text1' )
    ENDWITH

  • Joshizzle

    Check the sample code. Sparse should be .F. Assign the controlsource to the column unless you have a good reason to do the otherwise.
  • Amit Bansal

    >> *****===>>> Here I would like to add object class 'mytxt1' instead of text1

    YOu need something along these lines...

    WITH thisform.kgrdledger1
    .addcolumn(1)
    .columns(thisform.kgrdledger1.ColumnCount).name = "colAmount"
    WITH .colAmount
    .Addobject( 'mytxt1', 'mytxtclass' )
    .RemoveObject( 'text1' )
    ENDWITH
    .....



  • Aditya.P

    Thank you all!

  • Martijn Mulder

    A couple of issues here.

    First, as Cetin said, assign the controlsource to the COLUMN not the textbox. Second you are not making the textbox you added the current control before trying to assign it. If you are not going to remove the original textbox until after you add the new, you must make the new one the current control

    WITH .colMainFld
    .Addobject( 'txtMainFld', 'mytextbox' )
    .CurrentControl = 'txtMainFld'

    Then you should be able to set it up as you wish




  • Lima Beans

    You might also override grid's addcolumn code and add your own column with your controls in it. ie:

    Public oForm
    oForm = Createobject('myForm')
    oForm.Show

    Define Class myForm As Form
    DataSession = 2
    ShowTips = .T.
    Procedure Load
    Use employee
    Endproc
    Add Object myGrd As myGrid With RecordSource = 'Employee'
    Enddefine

    Define Class myGrid As Grid
    DeleteMark = .F.
    ReadOnly = .T.
    RecordMark = .F.
    ScrollBars = 3
    SplitBar = .F.
    Highlight = .F.
    HighlightRow = .F.
    Name = "grdMyGrid"

    Procedure AddColumn
    Lparameters nIndex, cAlias, cField
    Nodefault
    This.AddObject("clm"+cField,"myColumn", cAlias+"."+cField,nIndex)
    Endproc

    Procedure Init
    Lparameters tcRecordsource
    tcRecordSource = Iif(Empty(m.tcRecordSource),This.RecordSource,m.tcRecordSource)
    With This
    .ColumnCount = -1
    .RecordSource = tcRecordsource
    nOldColCount = .ColumnCount
    For ix = 1 To Fcount(tcRecordsource)
    .AddColumn(ix, tcRecordsource,Field(ix,tcRecordsource))
    Endfor
    .ColumnCount = nOldColCount
    .SetAll('Visible',.T.)
    Endwith
    Endproc

    Procedure When
    Set Cursor Off
    Endproc

    Procedure Valid
    Set Cursor On
    Endproc
    Enddefine

    Define Class myColumn As Column
    Resizable = .F.
    Movable = .F.
    Procedure Init
    Lparameters cControlSource, nIndex
    With This
    .ControlSource = cControlSource
    .ColumnOrder = nIndex
    .RemoveObject('Header1')
    .AddObject('myHeader','myHeader')
    .AddObject("myText","myGridTxtBox")
    .CurrentControl = "myText"
    .Sparse = .F.
    Endwith
    Endproc
    Procedure MouseMove
    Lparameters nButton, nShift, nXCoord, nYCoord
    With This.Parent
    lnActiveRow = Ceiling( ;
    ( nYCoord - (.Top + .HeaderHeight) ) / .RowHeight )
    lnActivecol = This.ColumnOrder - This.Parent.LeftColumn + 1
    .ActivateCell(lnActiveRow,lnActiveCol)
    Endwith
    This.myText.ToolTipText = ;
    iif(Type(This.ControlSource)='C', Eval(This.ControlSource),;
    iif(Type(This.ControlSource)='M', Mline(Eval(This.ControlSource),1),''))
    Endproc
    Enddefine


    Define Class myGridTxtBox As TextBox
    Procedure Init
    This.BorderStyle = 0
    Endproc
    Procedure Click
    This.SetFocus()
    Endproc
    Enddefine

    Define Class myHeader As Header
    Procedure Init
    This.Caption = Proper(Justext(This.Parent.ControlSource))
    Endproc
    Enddefine


  • haridorai

    Here I would like to add object class 'mytxt1' instead of text1

    In addition to the information that Andy gave you, do not forget to set the text box's visible property to .T.

    Objects instantiated using the .AddObject or .NewObject methods aree instantiated with their visible property set to .F.



  • How to add my own objects to a grid programatically