Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread A real nEWBie trying to sort a grid by clicking column headers
Thu, Sep 20 2012 1:38 AMPermanent Link

Jeff Cook

Aspect Systems Ltd

Avatar

Hi

Using the Trial Version - frantically evaluating EWB before the promo deal expires tomorrow...

I composed most of my stuff below before I found this in a post in Febuary:-

=====================
- The TGrid component now includes functionality for automatic sorting of
datasets by clicking on the column headers.  This works with bound grids
only.  There is also now a public Cells property that allows you to easily
get/set any cell using column/row coordinates.
======================

... BUT I can't see how to set up the sort functionality

My code below tries to do the same thing but does some strange things.   There are three problems:-

1.   My formcreate procedure correctly sorts the data, but doesn't mark the column header with a "^" or "V" like I thought it would.

2.  My column header procedure does some odd things (to me).

   First click on column sorts descending with a "^" mark - I was expecting ascending.
   Second click sorts ascending  with a "v" mark - I was expecting the opposite
   Third click appears to do nothing except remove the mark on that column.

3.  The code to remove the non clicked columns from the sort sequence doesn't work, they just remain marked.

The code is below.

Cheers

Jeff
===================================================
procedure TfrmMain.frmMainCreate(Sender: TObject);
begin
 dsOwners.Open;
 Database.Load(dsOwners);
 dsOwners.Columns['SortKey'].SortDirection := sdAscending;
 dsOwners.Sort;
end;

procedure TfrmMain.grdOwnersColumnHeaderClick(Sender: TObject; Column: TGridColumn);
var
 i: integer;
begin        
// first remove the sort from all but the clicked column

 for i:= 0 to dsOwners.Columns.Count - 1 do
   if dsOwners.Columns[i].Name <> Column.DataColumn then
     dsOwners.Columns[i].SortDirection := sdNone;

// for the clicked column, sort ascending unless
// already ascending, in which case sort descending

 if dsOwners.Columns[Column.DataColumn].SortDirection = sdAscending then
   dsOwners.Columns[Column.DataColumn].SortDirection := sdDescending
 else dsOwners.Columns[Column.DataColumn].SortDirection := sdAscending;
 dsOwners.Sort;

end;
===================================================
Mon, Sep 24 2012 1:43 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Jeff,

<< 1.   My formcreate procedure correctly sorts the data, but doesn't mark
the column header with a "^" or "V" like I thought it would. >>

You have to use the grid columns' SortDirection property to make sure that
the grid headers change accordingly, like this:

  Grid1.Columns[1].SortDirection := sdAscending;

etc.  Currently, the relationship between the grid column's SortDirection
property and the dataset is unidirectional, with the grid controlling the
process.

<< 2.  My column header procedure does some odd things (to me).

   First click on column sorts descending with a "^" mark - I was expecting
ascending.
   Second click sorts ascending  with a "v" mark - I was expecting the
opposite
   Third click appears to do nothing except remove the mark on that column.
>>

Yes, ascending is ^, descending is v, and a third click removes any sort at
all.

<< 3.  The code to remove the non clicked columns from the sort sequence
doesn't work, they just remain marked. >>

See above.

If you have any other questions, please let me know.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Sep 24 2012 2:07 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Jeff,

Just to let you know, since this was an easy fix I've gone ahead and changed
it for 1.01.  It will automatically have the grid reflect the dataset sort
column changes when they occur.  This was a basic oversight during 1.00
development that should have been addressed.

If you have any other questions, please let me know.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Sep 24 2012 4:48 PMPermanent Link

Jeff Cook

Aspect Systems Ltd

Avatar

Thanks, Tim

Cheers

Jeff
Image