Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change sort order in DBGrid at runtime

Status
Not open for further replies.

RonR51

Programmer
Apr 19, 2001
18
AU
I am having problems changing the sort order on a DBgrid at runtime (I am using paradox tables). I have pre-defined an index for each field I wish to sort on (in ascending order).

I can change which column to sort on by clicking in the column title without problems, but changing the order does not work.

I have stepped through my code and the index definition is getting changed correctly but it seems to lose the change when the Indexdefs are updated or the table is reopened. Here is my routine that is called from the OnTitleClick event.

procedure TTimeSeriesForm.Descend(DoDescending: boolean; Column: TColumn);
var
I: Integer;
begin
CatGrid.DataSource.DataSet.DisableControls;
with Datamodule2 do
begin
CSR.Close;
for I := 0 to CSR.IndexDefs.Count - 1 do
if (CSR.IndexDefs.Items.Fields = Column.FieldName) then
begin
if (Not DoDescending) then
CSR.IndexDefs.Items.DescFields := ''
else
CSR.IndexDefs.Items.DescFields := Column.FieldName;
CSR.IndexName := CSR.IndexDefs.Items.Name;
end;
CSR.Open;
end;
CatGrid.DataSource.DataSet.EnableControls;
end;


Thank RonR
 
Try this:


First in the IndexFieldnames of the TQuery or TTable fill some fields in, for multiple fields separate them by ;

Select the on TitleClick event of the DBGrid and fill in:

procedure TMyForm.dbGrid1TitleClick(Column: TColumn);
begin
myDtaMod.Mytable.IndexFieldNames := Column.FieldName;
end;

Regards Steven van Els
SAvanEls@cq-link.sr
 
Thanks Steven for the reply but I did not explain my problem well enough in my first post.

Your solution works fine when changing between columns that are already indexed as does my routine. But what I am trying to do is change between ascending and descending order on the same column.

From what I can see, the only way to change the definition of an index is with Database Desktop etc. at design time. I have tried making the relevant changes to the IndexDefs property of the table to switch between ascending/descending and they seem to be done when I step through the code, but they do not get saved permanently.

It looks as though will I need to define 2 indexes for each column that I want to sort by (1 for ascending and 1 for descending). Seems a very messy way to get a result.

Regards

Ron Roper
ronr@arrb.com.au
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top