Post by Pier Alberto GUIDOTTIMaybe this has already posted by many people: it would be a great thing if
records in grid could be ordered simply by clicking on the header of a
column.
Here's a weekend present, something I threw together in about half an
hour, and it could probably use some work. See the instructions in the
code (and watch for line wrapping and such):
/*
GridColumnIndexChange.cc
Programmer ...: Ken Mayer
Date..........: February 13, 2004
Notes.........: A custom grid that can be used to allow the
user to click on a columnHeading and
change the sort sequence of a rowset.
Note that this is not perfect, it only
works with grids that are datalinked
to a single rowset, and there are probably
some other issues that might cause problems.
Cobbled together based on a request in the
dBASE Wishlist newsgroup by Pier Alberto
Guidotti.
Usage Notes...: Make sure this is available as a custom
component:
set procedure to GridColumnIndexChange.cc
additive
Drag to a form.
Set the datalink.
Set the columns array (it won't work without it,
and currently we're not testing for that).
Run the form ...
Option........: grid.warnMessage -- this property will
cause a message to display on screen
for the user if the column heading selected
is for a column that doesn't have an index tag
by the fieldname ...
*/
class GridColumnIndexChange( oParent ) of grid( oParent ) custom
this.onOpen = class::SetColumnHeadings
this.WarnMessage = false
function SetColumnHeadings
// this should fire in the grid's onOpen, and set the
// column headings' onLeftMouseUp to fire the code
// 'ChangeIndex':
private aColumns
// use this so we know how many columns we have:
aColumns = this.getColumnOrder() // returns an array
for i = 1 to aColumns.size/2 // two columns in array
cColName = "column"+i
this.columns[ cColName ].headingControl.onLeftMouseUp =
class::ChangeIndex
next
return
function ChangeIndex(flags, col, row)
// This is a test, it is only a test
// for this to work for a 'custom grid', in the grid's
// onOpen we would need to loop through the
// columns, and set the headingControl's onLeftMouseUp
// event to this code ...
private nColumn, aColumns, cField, oRowSet, cOldIndex
nColumn = this.parent.currentColumn // get current column
aColumns = this.parent.getColumnOrder() // returns an array
cField = aColumns[ nColumn, 2 ] // fieldName
// Now we have the fieldname, try to set the
// index ... we're going to try just based on the
// fieldName ... there's no good way (without using
// a tableDef object) to find out if there's
// an index tag by a different name that will
// do the trick.
oRowSet = this.parent.dataLink
// in case it didn't work:
cOldIndex = oRowset.indexName
try
oRowset.indexName := cField
catch( Exception E )
if e.code # 239 // "Index does not exist"
msgbox( "Unexpected error: "+e.code+" -
"+e.message,"Error!", 16 )
else
// optional message:
if this.parent.warnMessage
msgbox( "No index is set for this field ...",
"Warning", 48 )
endif
endif
// error, set index to what it was before ...
oRowset.indexName := cOldIndex
endtry
return
endclass
---
Ken Mayer [dBASE, Inc.]
** Please respond ONLY in the newsgroups **
"Think OOP"
dBASE, Inc. website: http://www.dbase.com