• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1540
  • Last Modified:

Making all columns visible in Delphi Grid

Hi everyone.

I'm trying to program a quick way to make all of the columns in a grid visible, rather than the user having to individually click each of the checkboxes in the ColumnsQuickCustomization drop down. I know this can be done by individually calling each of the columns, but I would like to find a way around this since many of the grids have quite a large number of columns--plus I would be able to drop it into any grid with relatively little coding. I was thinking something along the lines of:

if TcxGrid.ColumnProperty.Visible := False then TcxGrid.ColumnProperty.Visible := True;

Would this be possible, or am I going about this the wrong way?
0
Norm-al
Asked:
Norm-al
  • 2
  • 2
2 Solutions
 
Geert GOracle dbaCommented:
you could use a popupmenu alongside the cxGridMenu to do that:
pas
 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, Menus,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridCustomTableView,
  cxGridTableView, cxGridCustomView, cxClasses, cxGridLevel, cxGrid;

type
  TForm1 = class(TForm)
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGrid1TableView1: TcxGridTableView;
    cxGrid1TableView1Column1: TcxGridColumn;
    cxGrid1TableView1Column2: TcxGridColumn;
    cxGrid1TableView1Column3: TcxGridColumn;
    cxGrid1TableView1Column4: TcxGridColumn;
    cxGrid1TableView1Column5: TcxGridColumn;
    cxGrid1TableView1Column6: TcxGridColumn;
    cxGrid1TableView1Column7: TcxGridColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    PopupMenu1: TPopupMenu;
    Showallcolumns1: TMenuItem;
    Hideallcolumns1: TMenuItem;
    procedure Showallcolumns1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Showallcolumns1Click(Sender: TObject);
var vw: TcxCustomGridView;
  I: Integer;
begin
  vw := cxGridPopupMenu1.Grid.ActiveView;
  for I := 0 to vw.DataController.ItemCount - 1 do
    TcxGridColumn(vw.DataController.GetItem(I)).Visible := TComponent(Sender).Tag = 1;
end;

end.

Open in new window


dfm
 
object Form1: TForm1
  Left = 245
  Top = 86
  Caption = 'Form1'
  ClientHeight = 525
  ClientWidth = 963
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object cxGrid1: TcxGrid
    Left = 88
    Top = 72
    Width = 713
    Height = 353
    TabOrder = 0
    object cxGrid1TableView1: TcxGridTableView
      NavigatorButtons.ConfirmDelete = False
      DataController.Summary.DefaultGroupSummaryItems = <>
      DataController.Summary.FooterSummaryItems = <>
      DataController.Summary.SummaryGroups = <>
      object cxGrid1TableView1Column1: TcxGridColumn
        Caption = 'A'
      end
      object cxGrid1TableView1Column2: TcxGridColumn
        Caption = 'B'
      end
      object cxGrid1TableView1Column3: TcxGridColumn
        Caption = 'C'
      end
      object cxGrid1TableView1Column4: TcxGridColumn
        Caption = 'D'
      end
      object cxGrid1TableView1Column5: TcxGridColumn
        Caption = 'E'
      end
      object cxGrid1TableView1Column6: TcxGridColumn
        Caption = 'F'
      end
      object cxGrid1TableView1Column7: TcxGridColumn
        Caption = 'G'
      end
    end
    object cxGrid1Level1: TcxGridLevel
      GridView = cxGrid1TableView1
    end
  end
  object cxGridPopupMenu1: TcxGridPopupMenu
    Grid = cxGrid1
    PopupMenus = <
      item
        GridView = cxGrid1TableView1
        HitTypes = [gvhtColumnHeader]
        Index = 0
        PopupMenu = PopupMenu1
      end>
    Left = 40
    Top = 200
  end
  object PopupMenu1: TPopupMenu
    Left = 40
    Top = 256
    object Showallcolumns1: TMenuItem
      Tag = 1
      Caption = 'Show all columns'
      OnClick = Showallcolumns1Click
    end
    object Hideallcolumns1: TMenuItem
      Caption = 'Hide all columns'
      OnClick = Showallcolumns1Click
    end
  end
end

Open in new window

0
 
fromerCommented:
Here is TcxGridTableView = class(TcxCustomGridTableView)

which has a property:

property Columns[Index: Integer]: TcxGridColumn read GetColumn write SetColumn;

and it's GetColumn method is

function TcxGridTableView.GetColumn(Index: Integer): TcxGridColumn;
begin
  Result := TcxGridColumn(Items[Index]);
end;

Items property is inherited from TcxCustomGridTableView, and it's declared as

property Items[Index: Integer]: TcxCustomGridTableItem read GetItem write SetItem;



So I will suggest functions like below...
//------------------------------------------------------------------------------------------------------
function TryMakeAllColumnsVisible(_View :  TcxCustomGridView) : Boolean; forward;
//------------------------------------------------------------------------------------------------------
procedure MakeAllColumnsVisible(_Grid : TcxGrid); overload;
var
  k : Integer;
begin
  for k := 0 to _Grid.ViewCount - 1 do
  begin
    TryMakeAllColumnsVisible(_Grid.Views[k]);
  end;
end;
//------------------------------------------------------------------------------------------------------
procedure MakeAllColumnsVisible(_View : TcxCustomGridTableView); overload;
var
  k : Integer;
begin
  for k := 0 to _View.ItemCount - 1 do
  begin
    _View.Items[k].Visible := True;
  end;
end;
//------------------------------------------------------------------------------------------------------
function TryMakeAllColumnsVisible(_View :  TcxCustomGridView) : Boolean;
begin
  if _View is TcxCustomGridTableView then
  begin
    MakeAllColumnsVisible(TcxCustomGridTableView(_View));
    Result := True;
  end
  else
  begin
    Result := False;
  end;
end;
//------------------------------------------------------------------------------------------------------
0
 
Geert GOracle dbaCommented:
nice fromer,
but a little complex ... no ?

is this part of a larger piece of code ?
0
 
fromerCommented:
I can see the
unit cxGrid,
unit cxGridCustomTableView,
etc...

The codes after "So I will suggest functions like below..." are written by me..
0
 
mlmccCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now