Link to home
Create AccountLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

drawing a triangle in dbgrid

Hello guys,

Maybe you can help me with this function that I have to draw a triangle in my dbgrid. This function is used to show how title was clicked and ordered.

I'd like to have both way, a draw for "asc" and another for "desc"

or maybe this code can be more simple, please, take a look at it:

You can test it with a simple grid linked in a dataset

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Query1: TQuery;
    BitBtn1: TBitBtn;
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure DrawMarc(Grid: TDbGrid; Column: TColumn; MaxColumn: integer;
  ini: integer=0);
  var x, i : integer;

  procedure Draw(xx: integer);
  begin
    grid.Canvas.FillRect(Rect( xx, 6, xx + 6, 14));
    //draw a triangle
    grid.Canvas.MoveTo(xx,6);
    grid.Canvas.LineTo(xx + 6,6);
    grid.Canvas.LineTo(xx + 3,12);
    grid.Canvas.LineTo(xx,6);
  end;

  procedure Paint(min, top: integer);
    var max, lin : integer;
  begin
    max := min + 7;
    lin := 2;
    While min +1 < max do
    begin
      if lin = 2 then
      begin
        inc(min);
        dec(max);
        lin := 1;
      end
      else
        inc(Lin);
      inc(top);
      grid.Canvas.MoveTo(min, top);
      grid.Canvas.LineTo(max, top);
    end;
  end;

begin
  x := 0;
  grid.Canvas.Brush.Color := $00D0EFF2;

  // limpa marca antiga
  for i := 0 to  MaxColumn -1 do
  begin
    x := x + Grid.Columns.Items[i].Width;
    if i > 0 then
      x := x + 1;  // soma 1 px para o desenho da linha.

    if i < ini then
      continue;

    if Grid.Columns.Items[i] = Column then
    begin
      grid.Canvas.Pen.Color := clOlive;//$00D0EFF2;
      Draw(x - 10);
      Paint(x - 10 ,6);
    end
    else
    begin
      grid.Canvas.Pen.Color := clsilver;
      Draw(x - 10);
    end;
  end;
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  drawMarc(DBGrid1,column,10, 0);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  query1.Open
end;

end.

Open in new window

Avatar of Thommy
Thommy
Flag of Germany image

Have a look at following solution....
dbGrid &Ā dataset sort question
You can also try out SMDBGRID on www.scalabium.com
To draw sort arrows you basically need to override Drawcell event for the heading cells...
How to draw an arrow into the header of a TDBGrid
I myself use TMS components, which have this sorting functionality built-in as standard...
TMSSoftware.com

...and just 2 older EE questions concerning that item:
TDBGrid Title Header Glyphs
DBGrid: Howto show up or down for sorting in the title
Avatar of Sinisa Vuk
Add variable with three state:
0 - no sort mark
-1 - sort up (desc)
1 - sort down (asc)
.. then follow Thommys How to draw an arrow ... suggestion.
I use ImageList to do this with Imglist.Draw method... because I'm flexibile
this way to have more complex shapes than triangles... (and think it is faster then line method)
Avatar of hidrau

ASKER

The code I posted works pretty good, I only need to know a way to reverse the triangle.

Or maybe, instead of drawing a triangle in my dbgrid, I could use a Timage with two image, one for up and another to down. But I don't know how to put the image from my Timage into my head grid.

I checked all suggestion but I can use them and some didn't work.
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of hidrau

ASKER

thanks
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.