Drag and Drop Row Arrangement in DataGrid

Bob Sneidar bobs at twft.com
Thu Mar 3 14:07:48 EST 2011


I just tried this on my own DataGrid and I was able to drag and drop with a column sort enabled. When I do this, the column sort property is still set, but the column is unsorted, so this does not happen auto-magically. 

I think the key then is to set the Sort By Column property to empty before you change the contents of the datagrid. You would do this in the DragDrop or the dragReorderDrop handler of the destination datagrid. (You can only use one or the other in a script one takes priority, I think it's dragReorderDrop). 

What is rather unfortunate at this time is that the API on lessons.runrev.com list this command under data grid properties, and then not the actual name of the command, without spaces. I give you: 

"sort by column
- The column that the table data is currently being sorted by. You can set this property to sort by a new column."

You would use:
sortByColumn empty to set the sort to nothing. Otherwise, if you do something like set the dgText (or dgData if I am not mistaken) the refresh will sort the datagrid which is probably not the behavior you want in a drag and drop situation. 

So my dragReorderDrop handler looks like this. Keep in mind I have modified it to work for dragging and dropping a single cell in a single column. But the relevant code is that I issue the command "sortByColumn empty" just before I modify the data in the datagrid. 

on DragReorderDrop pOriginatingIndex, pStartLine, pDroppedOnLine
    local tTheDragData
    put the dragData["private"] into tTheDragData
    put the dragsource into theSource
    put the dragdestination into theDestination
    
    ## Reorder Items
    -- We don't want to move whole lines, only the items in a column
    -- get the control the user clicked on
    put line 2 of tTheDragData into theControlID
    put word 1 of the short name of theControlID into theColumn
    
    put the dgIndex of thesource into theSourceID
    put the dgIndex of thedestination into theDestinationID
     
    put the dgDataOfIndex[theSourceID] of me into aSourceData
    put the dgDataOfIndex[theDestinationID] of me into aDestinationData
    
    if aSourceData is not an array or aDestinationData is not an array then
        answer "What the hell is going on here??!?"  & cr & \
                theSource & cr & theDestination with "I have no idea!!!"
    end if
    
    sortByColumn empty -- the magical line of code	
    put aSourceData[theColumn] into temp
    put aDestinationData[theColumn] into aSourceData[theColumn]
    put temp into aDestinationData[theColumn]
    set the dgDataOfIndex[theSourceID] of me to aSourceData
    set the dgDataOfIndex[theDestinationID] of me to aDestinationData
end DragReorderDrop

BTW if you use the message box to issue the command:

edit the script of the behavior of group "dgMyDataGrid" 

You can see what is going on behind the scenes and find out all the stuff you can do with a data grid. I am learning a lot that way. 

Bob



More information about the use-livecode mailing list