Topic: How to sort

Unfortunately, the component TableGrid and SQLite does not support proper text sorting when characters like Č, Ä and so on are present.
To solve this problem, you need to use MySQL and disable the built-in sorting in the component.



Script

procedure Form1_GridSearch_OnChange (Sender: TObject);
begin
    // you should disable sort option for all columns
    Form1.GridSearch.Columns[0].Options := Form1.GridSearch.Columns[0].Options - coCanSort;
    Form1.GridSearch.Columns[1].Options := Form1.GridSearch.Columns[1].Options - coCanSort;
end;

procedure Form1_GridSearch_OnHeaderClick (Sender: TObject; ACol: Integer);
var
    sField: string;
    Ascending: boolean;
begin
    Ascending := True;
    if Form1.GridSearch.Tag = ACol+1 then Ascending := False;

    if (Form1.GridSearch.Columns[ACol].FieldType <> 'CALC') and (Form1.GridSearch.Columns[ACol].FieldType <> 'AUTOINC') and (Form1.GridSearch.Columns[ACol].FieldType <> 'CHECKBOX') then
    begin
        sField := '`' + Form1.GridSearch.Columns[ACol].TableName + '`.`' + Form1.GridSearch.Columns[ACol].FieldName+'`';

        if Ascending then Form1.GridSearch.dbCustomOrderBy := sField + ' ASC'
        else Form1.GridSearch.dbCustomOrderBy := sField + ' DESC';
        Form1.GridSearch.dbUpdate;
        Form1.GridSearch.Columns[ACol].Sorted := True;
    end;

    if Ascending then
    begin
        Form1.GridSearch.Tag := ACol+1;
        Form1.GridSearch.Columns[ACol].SortKind := skAscending;
    end else
    begin
        Form1.GridSearch.Tag := 0;
        Form1.GridSearch.Columns[ACol].SortKind := skDescending;
    end;
end;

Project example:

Post's attachments

Attachment icon MySQL sorting eu language.zip 28.62 kb, 387 downloads since 2021-03-15 

Dmitry.