TwwDataGrid: Custom title button sorting of some fields.
Given a dataset attached to a grid, how can I manually sort fields the grid has determined to be not sortable while maintaining usual sorting look?
If an event passes the field name, I can adjust dataset SQL to order by the field. I need TwwDataGrid to display the sorting arrow & maintain descending value as if it's an auto-sorting column.
This is the beginnings of my attempt to code special sorting handling for a field the grid can't sort, but SQL can. It falls way short. I figure there has to be an easier way.
procedure TOrdersFrm.LinesGrdTitleButtonClick(Sender: TObject; AFieldName: string);
begin
inherited;
var lTitleButton := LinesGrd.ColumnByName(sLineDesc).HeaderTitleButton;
if lTitleButton <> nil then
begin
if SameText(aFieldName, sLineDesc) then
begin
begin
TPath(lTitleButton.ImageForButton).Fill.Color :=
THackwwDataGrid(LinesGrd).GetEffectiveTitleIconColor;
if LinesGrd.SortDescending then
begin
lTitleButton.ImageForButton.RotationAngle := 180;
end
else
begin
lTitleButton.ImageForButton.RotationAngle := 0;
end;
THackwwGridTitleButton(lTitleButton).SetImageForButtonVisible(True);
end;
end
else
begin
THackwwGridTitleButton(lTitleButton).SetImageForButtonVisible(False);
end;
end;
end;
-
If I understand you correctly, you wish to preserve the UI appearance that indicates sort-ability of the column even when the grid would suppress that display because it is incapable of doing the sorting in its usual way.
You can make the SQL achieve the sorting, but you want this to be triggered the way a user would sort by clicking on a column header.
I have no experience with replacing title button images, but doing that sounds like the most natural approach and hacking the code of TwwDBGrid would be the last thing I would try.
You say your approach "falls way short" but can you describe how it is inadequate? Have you looked for clues in the TwwDBGrid code to see how it manages the presence of the sort arrows?
-
Your understanding is correct. Grid sorting performs exactly the same except the actual sorting is decided to do default sorting or call an OnCustomSort event.
Grid source would be something like ...
if assigned(fOnCustomSort) then
fOnCustomSort(Self)
else
DoDefaultSort;Much of what I wrote was extracted from the grid source. The arrow almost works as it should. When clicking the custom sorted column after a std sort column, the std sort column doesn't clear its arrow.
Also, after reading more grid source, there is more to do, e.g. clearing open editors. I pulling in a lot of code from source and cracking open classes for protected members to get custom sort capability. It's getting a little messy.
-
You can control the icon with the following properties.
procedure TDataGridCustomColorsForm.Button1Click(Sender: TObject); begin dgfish.sortfieldname:= 'LengthIn'; // Set which column should show icon dgfish.RefreshTitleButtons(); end;
You may also want to set the grid's SortDescending property to true if the grid is descending order.
Please sign in to leave a comment.
Comments
4 comments