DAJMonk
asked on
DBGrid Column sorting: Field Index out of Range error
Hello experts,
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(AF ieldName: String; dsGrid: TClientDataSet): boolean;
var
ix: TIndexDef;
begin
//Reverse column sorting with each click
if dsGrid.IndexFieldNames <> '' then
begin
//Sort Descending
dsGrid.IndexFieldNames := '';
ix := TIndexDef.Create(dsGrid.In dexDefs, AFieldName, AFieldName, [ixDescending]);
dsGrid.IndexName := ix.Name;
end
else
begin
//Sort Ascending
dsGrid.IndexName := '';
dsGrid.IndexFieldNames := AFieldName;
end;
end;
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!
-DAJMonk
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(AF
var
ix: TIndexDef;
begin
//Reverse column sorting with each click
if dsGrid.IndexFieldNames <> '' then
begin
//Sort Descending
dsGrid.IndexFieldNames := '';
ix := TIndexDef.Create(dsGrid.In
dsGrid.IndexName := ix.Name;
end
else
begin
//Sort Ascending
dsGrid.IndexName := '';
dsGrid.IndexFieldNames := AFieldName;
end;
end;
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!
-DAJMonk
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.