Method 1: (Write code without programming)
Reference Unit Ehlibcds in the program
To set the properties of a Dbgrideh:
ColumnDefValues.Title.TitleButton = True
Optionseh = [Ghautosortmarking, dghmultisortmarking]
Sortlocal = True
Reference:
The following translations are from Ehlib/dataservice/readme.txt
"in the Ehlib DataService folder, there are some files that enable the data set to be automatically sorted. Tdbgrideh uses the related objects in these files to implement automatic sorting when the sort mark changes.
"If you change the sorting tag of the data grid and headings without writing the Onsortmarkingchanged event, the data grid will try to sort the data automatically."
Dbgridehdataservice tries to find specific objects in Tdataset that can be sorted by Getdatasetfeaturesfordataset. As you know, Tdataset does not support data ordering, but its derived objects, tquery or TClientDataSet, allow for data sorting.
"Using procedure Registerdatasetfeatureseh, you can register the Tdatasetfeaturesehclass class to sort the specified datasets.
"Ehlib has implemented classes that can sort data in Tquery, Tadoquery, and TClientDataSet objects. Simply add ehlib through the uses clause ... (Ehlibbde,ehlibado,ehlibcds) In one of the units of your project, the data grid connected to them will automatically sort the dataset. Ehlibbde, Ehlibado, Ehlibcds implements initialization by calling the Registerdatasetfeatureseh procedure in a cell dataset.
for other types of datasets, you must write and register new objects that can be sorted by that dataset. The writing process t[your dataset]datasetfeatureseh.applysorting, and you can access the columns that use the Sortmarkedcolumns attribute for direct ordering. You can look at the examples in the Dbutilseh cell to understand how to write t[your DataSet]datasetfeatureseh class and view the Ehlibbde unit to see How to register t[your DataSet]datasetfeatureseh class.
Engine Data Set unit file
BDE tquery Ehlibbde
ADO tadoquery Ehlibado
clientdataset tclientdataset Ehlibcds
dbexpress tsqlquery ehlibdbx
Method 2:
in my Ontitleclick event:
Case Column.Title.SortMarker of
SmNoneEh:Column.Title.SortMarker: = Smdowneh;
SmDownEh:Column.Title.SortMarker: = Smupeh;
SmUpEh:Column.Title.SortMarker: = Smnoneeh;
end;
in my onsortmarkingchanged event:
showmessage (' marker changed! ');//Never run. Why???????
//Add an ORDER BY statement
in my Dbgrideh property page, Optionseh is set:
dghautosortmarking=true;
Please use a Ehlib2.1 expert to point out: How to set the Onsortmarkingchanged event to occur.
in Ehlib's Demo1, there are no ontitleclick events, and I don't know why I can sort them.
set Dbgrideh1.column[i] Titlebutton: = True
Dbgrideh has an attribute frozencols
you set it to 1, your first column is fixed.
set to 2, the first two columns are fixed, in turn ...
procedure Tbranch_type_frm.dbg1titleclick (Column:tcolumneh);
begin
if not (adoquery1. IsEmpty) then Adoquery1. Sort:=column.field.fieldname;
end;
;//Sort
The second problem: The demo does not program Ontitleclick, the Ontitlebtnclick programming is commented, as follows:
{case Column.Title.SortMarker of
SmNoneEh:Column.Title.SortMarker: = Smdowneh;
SmDownEh:Column.Title.SortMarker: = Smupeh;
SmUpEh:Column.Title.SortMarker: = Smnoneeh;
end;}
and the effect of this program is only to control the sorting display superscript, does not produce the actual sort of the fruit
my own workaround:
procedure Tform1.dbgrideh1titleclick (Column:tcolumneh);
var
I:integer;
s:string;
fieldstr:string;
oldsql,newsql:string;
begin
lockwindowupdate (self). Handle);
s: = ';
I: = Column.index;
fieldstr: = Dbgrideh1.columns[i]. FieldName;;
if Dbgrideh1.columns[i]. Title.sortmarker = Smnoneeh Then
begin
Dbgrideh1.columns[i]. Title.sortmarker: = Smdowneh;
s: = ' ORDER by ' + fieldstr;
End
Else
if Dbgrideh1.columns[i]. Title.sortmarker = Smdowneh Then
begin
Dbgrideh1.columns[i]. Title.sortmarker: = Smupeh;
s: = ' ORDER BY ' + fieldstr + ' Desc ';
End
Else
begin
Dbgrideh1.columns[i]. Title.sortmarker: = Smdowneh;
s: = ' ORDER by ' + fieldstr;
end;
oldsql: = SimpleDataSet1.DataSet.CommandText;
I: = pos (' ORDER by ', oldsql);
if I <> 0 then
newsql: = Copy (oldsql,0,i-1) + S
Else
Newsql: = oldsql + ' + S;
If newsql <> oldsql THEN BEGIN//If not equal start
simpledataset1.packetrecords: = ten;
simpledataset1.active: = False;
SimpleDataSet1.DataSet.CommandText: = Newsql;
simpledataset1.active: = True;
end;
lockwindowupdate (0);
end;
Some parameters:
flat:true XP Style
Options-dgrowselect Select one to display a row (will automatically set edit to False)
the DisplayFormat #,# of the field #0.00;-#,# #0.00 formatted Digital display
The checkboxes is set to the CheckBox control. If it is an integer then set footer in Keylist and picklist, one is the value one is the display
Dbgrideh Click the header sort method