TAdvStringGrid

Example 69 : Using Windows Vista style column header dropdown menus

vcl grid

The Windows Vista file explorer introduces interesting and user friendly filtering & sorting capabilities via column headers. By using TAdvStringGrid and TAdvStickyPopupMenu (available in the TMS VCL UI Pack) it is possible to add a similar interface to your applications.

To achieve this, set grid.Look = glVista. This will change the fixed cell colors to appear with mirrored gradients as well as hover & down colors. To have a dropdown button on fixed cells, set grid.ControlLook.FixedDropDownButton to true. Hovering on fixed cells is enabled by setting grid.HoverFixedCells to hfFixedRows.

Drop a TAdvStickyPopupMenu on the form. The menu items are set to allow to configure a filter for values 0..25, 26..50, 51..75 and 76..100. Two items are added to invoke ascending or descending sorting.

The grid is initialized with values between 0 and 100 with code:

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  advstringgrid1.RandomFill(false,100);
  advstringgrid1.SortSettings.Show := true;
  advstringgrid1.FixedRowAlways := true;
  for i := 1 to advstringgrid1.ColCount - 1 do
    advstringgrid1.Cells[i,0] := 'Col ' + inttostr(i);
end;
 
When the dropdown button on a fixed cell is clicked the event OnFixedDropDownClicked is triggered and from this event handler the AdvStickyPopupMenu is shown:  

procedure 
TForm1.AdvStringGrid1FixedDropDownClick(Sender: TObject; ACol,
ARow: Integer; var AMenu: TPopupMenu; var KeepFixedCellHighlighted: boolean);
var
  r: TRect;
  pt: TPoint;
begin
  r := advstringgrid1.CellRect(ACol,ARow);
  pt := Advstringgrid1.ClientToScreen(Point(r.Right - 16, r.Bottom));
  FilterColumn := ACol;
  AdvStickyPopupMenu1.ShowMenu(pt.X,pt.y);
  KeepFixedCellHighlighted := true;
end;
 
Until the menu hides, the fixed cell highlighting is forced by setting the parameter KeepFixedCellHighlighted to true. When the menu is hidden, this fixed cell highlighting is removed again with code:

procedure 
TForm1.AdvStickyPopupMenu1MenuHide(Sender: TObject);
begin
  advstringgrid1.ResetFixedCellHighlight;
end;


Handling the sorting is done from the MenuItemClick event:

procedure TForm1.AdvStickyPopupMenu1ItemClick(Sender: TObject; Index: Integer);
begin
  if Index = 5 then
    advstringgrid1.Sort(FilterColumn, sdAscending);
  if Index = 6 then
    advstringgrid1.Sort(FilterColumn, sdDescending);
end;


and the filtering is invoked from the MenuItemCheck event:

procedure TForm1.AdvStickyPopupMenu1CheckClick(Sender: TObject; Index: Integer; Checked: 
boolean);
var
  fltr: string;

  function AppendFilter(s:string): string;
  begin
    if fltr = '' then
      fltr := s
    else
      fltr := fltr + ' | ' + s;
  end;

begin
  if Index in [0..3] then  // filter checkbox is choosen
  begin
    fltr := '';
    advstringgrid1.FilterActive := false;  // remove 
filtering
    AdvStringGrid1.Filter.Clear;

    // build new filter
    if AdvStickyPopupMenu1.MenuItems[0].Checked then
      AppendFilter('(<=25)');

    if AdvStickyPopupMenu1.MenuItems[1].Checked then
      AppendFilter('(>25 & <=50)');

    if AdvStickyPopupMenu1.MenuItems[2].Checked then
      AppendFilter('(>50 & <=75)');

    if AdvStickyPopupMenu1.MenuItems[3].Checked then
      AppendFilter('(>75 & <=100)');

    if fltr <> '' then  // apply new filter
    begin
      with advstringgrid1.Filter.Add do
      begin
        condition := fltr;
        column := FilterColumn;
      end;
      AdvStringGrid1.FilterActive := true;
    end;
  end;
end;


Delphi project & source files for downloading included in the main demos distribution for Delphi.