DBGRID Click the title automatic sort

xiaoxiao2021-04-07  376

Procedure TFORM1.DBGRID1TITLECLICK (Column: Tcolumn);

VAR

s, cfieldname: string;

i: integer;

DataSet: TDataSet;

Begin

Try DataSet: = column.grid.datasource.DataSet;

If not dataset.active kiln

if Column.field.fieldkind = fklookup the

cfieldname: = column.field.keyfields

Else

if Column.field.fieldkind = fkcalculate THEN

cfieldname: = column.field.keyfields

Else

cfieldname: = column.fieldname;

If DataSet IS TCUSTOMADADATASET THEN

Begin

S: = tcustomadodataset (dataset) .sort;

IF s = '' THEN

Begin

S: = cfieldname;

END ELSE

Begin

IF Pos (cfieldname, s) <> 0 THEN

Begin

I: = POS ('DESC', S);

IF i <= 0 THEN

Begin

S: = S 'DESC';

END ELSE

Begin

Delete (S, I, 4);

END;

END ELSE

Begin

S: = cfieldname;

END;

END;

TCUSTOMADODATASET (DATASET) .SORT: = S;

END ELSE

If DataSet IS TclientDataSet Then

Begin

IF TclientDataSet (Dataset) .indexfieldNames <> 'THEN

Begin

I: = TclientDataSet (Dataset) .indexdefs.indexof ('i' column.fieldname);

IF i = -1 Then

Begin

With TclientDataSet (Dataset) .indexdefs.AddIndexdef Do

Begin

Name: = 'I' column.fieldname;

Fields: = column.fieldname;

DESCFIELDS: = column.fieldname;

END;

END;

TclientDataSet (Dataset) .indexfieldnames: = '';

Tcl atdataset (Dataset) .indexName: = 'i' column.fieldname;

END ELSE

Begin

TclientDataSet (Dataset) .indexname: = '';

TclientDataSet (Dataset) .indexfieldnames: = column.fieldname

END;

END;

Except

END;

END;

转载请注明原文地址:https://www.9cbs.com/read-132582.html

New Post(0)