Page 1 of 1

Special behavior in DataGrid Columns

Posted: Wed Feb 17, 2010 8:14 pm
by lohill
I am using a datagrid with 8 columns and want the user to be able to modify five of those columns. I have created a 'My Default Column Behavior' button which is giving me most of what I want but not everything. At this point I have modified the command EditValue and the MouseDoubleUp script as shown below:

Code: Select all

-- Data grid will call this if a user action asks to edit cell content.
command EditValue
   put the dgColumn of me into tCol
   if offset(tCol,"Shares To Sell,Price/Share,Commission,Amount,Sell Date") > 0 then
      EditFieldText the long id of me, the dgIndex of me, the dgColumn of me
   end if
end EditValue


on mouseDoubleUp pMouseBtnNum
   if pMouseBtnNum is 1 then
      put the dgColumn of me into tCol
      if offset(tCol,"Shares To Sell,Price/Share,Commission,Amount,Sell Date") > 0 then
         if the dgProps["allow editing"] of the dgControl of me \
                and the dgColumnIsEditable[the dgColumn of me] of the dgControl of me then
            -- Edit field contents if the user double-clicks
            EditCellOfIndex the dgColumn of me, the dgIndex of me
            exit mouseDoubleUp
         end if
      end if
   end if
   pass mouseDoubleUp
end mouseDoubleUp
There are two behaviors that I would still like to accomplish. The first is that when a user double clicks on one of the cells that can be edited, I would like to have the text in that cell be selected rather than having the cursor at the end of the text. If it were selected the user would not have to back space it out or double click again to type over it.

The other behavior I would like to have is that when a user modifies one of the columns 'Shares To Sell', 'Price/Share' or 'Commission' then the Amount would automatically be calculated (provided all the necessary values were present). This is all compounded by the fact that I am working in a window that is modal so tracing and message watching is not very easy to do.

Does anyone have any suggestions for getting these behaviors working?

Thanks in advance,
Larry

Re: Special behavior in DataGrid Columns

Posted: Thu Feb 18, 2010 4:19 am
by trevordevore
Larry,

Here is a lesson on selecting all text in a cell when it is open for editing: How Can I Select The Text in the Edit Field When It Opens?

As for updating one column when another is changed - you can recalculate the column value when the editor closes. This lesson shows how to store data in an external data source but the code could be modified to do what you want: How Do I Save Changes The User Makes In An Editor Field To An External Data Source?

In CloseFieldEditor just grab the new value of the field, use GetDataOfIndex to grab the values of the other columns to use in the equation, calculate the new value and use SetDataOfIndex to update the Amount column. Something like this (untested) in a Data Grid group script might work:

Code: Select all

on CloseFieldEditor pFieldEditor
    put the dgColumn of the target into theColumnBeingEdited
    put the text of pFieldEditor into theNewValue
    
    switch theColumnBeingEdited
        case "shares to sell"
            ## GET OTHER COLUMNS FOR CALCULATION
            put GetDataOfIndex(the dgIndex of me, "price/share") into thePricePerShare
            ...
            ## UPDATE AMOUNT
            SetDataOfIndex the dgIndex of me, "amount", SOME * EQUATION...
            
            ## DATA GRID WILL REDRAW AFTER THIS HANDLER FINISHES
            break
    end switch
end CloseFieldEditor

Re: Special behavior in DataGrid Columns

Posted: Thu Feb 18, 2010 10:36 pm
by lohill
Thanks Trevor,

Your first suggestion was perfect and I had the selected text working in minutes.

I had to work a little harder on the CloseFieldEditor suggestion but with a little tweaking of your code I have it working fine now.
I think the changes that I made boiled to to changing your 'the dgIndex of me' to 'the dgIndex of the target'. Being a bit of a novice I have to take things in smaller steps and I ended up with something like:

put the dgIndex of the target into theIndex
put GetDataOfIndex(theIndex, "price/share") into thePricePerShare

Any way, I am very thankful for your help. It is a wonderful feeling working in REV and knowing that there are many people who know what they are doing and are willing to help.

Regards,
Larry

I need a little more time to work it out for myself but there is another 'behavior' that I would like to achieve with your DataGrid. In Microsoft Excel when you select a series of cells, the sum of those cells shows in a field below the grid. I would like to be able to do that. I would restrict my wishes to a single column or partial column of contiguous cells and it would be nice if the cells were hilited and the sum available for displaying somewhere. Right now I'll just ask if the task is possible or not? If it is possible I'll get back in another thread.

Re: Special behavior in DataGrid Columns

Posted: Thu Feb 18, 2010 10:49 pm
by trevordevore
Sorry about using the dgIndex of me in the sample code. When in the group script you have to use 'the target'.

As for summing values in a column - There is a lesson that shows how to do this for an entire column: How Do I Get Aggregate Values for Columns?.

Just modify the code in the lesson so that it uses the dgHilitedIndexes of the Data Grid.