Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread sorting DBISAMDBGrid on column click
Tue, Jun 13 2006 11:10 AMPermanent Link

Nathan
Does anyone know how to sort a dbgrid based on a column title click?  I can't seem to find any answers...the only ones deal with ADO which I am not using.

Thanks
Tue, Jun 13 2006 11:45 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Nathan


Simply set the table indexname or indexfieldnames to the correct value. If you have an index that corresponds to the column then something like

table.indexfieldnames := column.fieldname

should work.

Roy Lambert
Tue, Jun 13 2006 3:01 PMPermanent Link

Jeff Cook
Nathan <_> wrote on Tue, 13 Jun 2006 11:10:57 -0400

>Does anyone know how to sort a dbgrid based on a column title click? I can't seem to find any answers...the only ones deal with ADO which I am not using.
>
>Thanks
>
Nathan


As Roy says for a table.  The following is an example of what I do if the datasource is a query

Cheers

Jeff
=================================
procedure TmainForm.TenantGridTitleClick(Column: TColumn);
var
 i: integer;
 TenantCode, FieldName: string;
begin
 FieldName := TenantGrid.Columns[Column.Index].FieldName;
 with qryTenants do
 begin
   TenantCode := qryTenantsTenantCode.AsString;
   Close;
   SQL.Strings[9] := 'ORDER BY ' + FieldName;
   if FieldName <> 'TenantCode' then SQL.Strings[9] := SQL.Strings[9] + ' NOCASE';
   Open;
   Locate('TenantCode', TenantCode, []);
 end;
 for i := 0 to TenantGrid.Columns.Count - 1 do
 begin
   if i = Column.Index then TenantGrid.Columns.Items[i].Title.Font.Color := clRed
   else TenantGrid.Columns.Items[i].Title.Font.Color := clWindowText;
 end;
end;
--
Jeff Cook
Aspect Systems Ltd
Phone: +64-9-424 5388
Skype: jeffcooknz
www.aspect.co.nz



Tue, Jun 13 2006 5:14 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Nathan,

<< Does anyone know how to sort a dbgrid based on a column title click?  I
can't seem to find any answers...the only ones deal with ADO which I am not
using. >>

Do you want to actually sort the dataset, or just set the active index to a
matching index for the column that is clicked ?

--
Tim Young
Elevate Software
www.elevatesoft.com

Wed, Jun 14 2006 4:17 PMPermanent Link

Nathan
Ok, I have a sql query search form that shows results in a dbgrid.  When the column title is clicked, I want the display of the data in the grid to be sorted...not the query itself.


Nathan


"Tim Young [Elevate Software]" <timyoung@elevatesoft.com> wrote:

Nathan,

<< Does anyone know how to sort a dbgrid based on a column title click?  I
can't seem to find any answers...the only ones deal with ADO which I am not
using. >>

Do you want to actually sort the dataset, or just set the active index to a
matching index for the column that is clicked ?

--
Tim Young
Elevate Software
www.elevatesoft.com

Wed, Jun 14 2006 4:28 PMPermanent Link

"Robert"

"Nathan" <_> wrote in message
news:F6D57930-66F4-48D0-8986-CCA8FFE4CA93@news.elevatesoft.com...
> Ok, I have a sql query search form that shows results in a dbgrid.  When
> the column title is clicked, I want the display of the data in the grid to
> be sorted...not the query itself.
>

Then you need a grid that has sorting capability (devExpress I think is
one).

To accomplish the same using the standard DBGrid, you need to re-sort the
dataset. With DBISAM, it is easier than you'd think. Do a select INTO a
memory table, then add the appropriate indexes to the memory table. Then on
click change index name as others have suggested.

It is just a few lines of code, and much faster and cleaner than sorting
large amounts of data, or having to reselect a query to change the order by.
The change in sequence with a memory table is instantaneous.

Robert


Image