I'm trying to create code that would allow one to sort ascending/descending on a column in a DBGrid. This needs to work on multiple DBGrids, and especially on forms that have more than one DBGrid.
This is my code so far:
function TForm1.DBGridToggleSort(AFieldName: String; dsGrid: TClientDataSet): boolean;
//Reverse column sorting with each click
if dsGrid.IndexFieldNames <> '' then
dsGrid.IndexFieldNames := '';
ix := TIndexDef.Create(dsGrid.IndexDefs, AFieldName, AFieldName, [ixDescending]);
dsGrid.IndexName := ix.Name;
dsGrid.IndexName := '';
dsGrid.IndexFieldNames := AFieldName;
In that function, I'm passing the AFieldName (generated from the OnTitleButtonClick event) and the DBGrid's Dataset. It works fine for the most part, on forms with only one DBGrid or on the first DBGrid in a form with multiple DBGrids.
The real problem I'm having is making it work on forms with multiple DBGrids. In those cases, if I pass the dataset from the second or third DBGrid, then I get an EDataBaseError with the message "CDSDataSet: Field Index out of range". I don't get that error if I pass the dataset from the first DBGrid. I can't for the life of me figure out why it won't work with the second or third DBGrid on the form.
Additionally, using the TQuery method is out of the question as the datasets I'm operating here are in the thousands of records, and that method would otherwise overload the database server. So, I appreciate any fresh insights into this problem. Thank you!