Topic: TableGrid Alternating Row Colour Procedure

Hello.
I've created two procedures. ShowAlternateRowColours and ClearAlternateRowColours. They turn on/turn off alternating row colours in a TableGrid. As I want to use these procedures for all TableGrids I create, how can I call these procedures and pass them a tablegrid object? Ideally, it would look something like the code stub below. I'm fairly certain I'll need to instantiate a TableGrid object and pass that into the procedure, but I don't know how to do that. Many thanks.

procedure ShowAlternateRowColours(<TableGrid>);
var
   i,k: integer;
begin
     for i := 0 to frmExpenseDS.<TableGrid>.RowCount - 1 do
         begin
            for k := 0 to frmExpenseDS.<TableGrid>.Columns.Count - 1 do
              begin
                frmExpenseDS.<TableGrid>.Cell[k,i].Color := $00F0F0F0;
              end;
         end;
end;

Re: TableGrid Alternating Row Colour Procedure

Hello Clyde

If I have well understood what you want (Color the grid one line out of two), you could use this code

procedure Form1_TableGrid1_OnChange (Sender: string);
var  c,q     : integer;
     iRow,iCol : integer;
begin
        c := Form1_TableGrid1.RowCount - 1;         // On colorie la grille une ligne sur deux
      q := Form1_TableGrid1.Columns.Count-1;
      for iRow := 0 to c do
         for iCol := 0 to q do
          begin
             if iRow mod 2 = 0 then Form1_TableGrid1.Cell[iCol,iRow].Color := clSkyBlue;
          end;
end;

Does this help you ?

JB

Re: TableGrid Alternating Row Colour Procedure

@jean - Many thanks for your response. I mistakenly included the wrong procedure in my previous post. My apologies. I have created the code below and it works fine for colouring alternate TableGrid rows. My question is how do I pass a TableGrid object (and the Form object) into the procedure so I can use the one procedure to colour alternate TableGrid rows no matter what form they live on in my project? If I had 20 forms in my project, each with a TableGrid on them, would I have to duplicate this procedure 20 times? Ideally, so I didn't have to duplicate code for each Form/Tablegrid object, I would want to call just one procedure and pass into it the form (on which the TableGrid object lives) and the TableGrid object who's rows I want to colour. The procedure's signature would perhaps look like this:

AlternateRowColours(<The Form object that holds the TableGrid object>,<The TableGrid object whose rows I want to colour>)

I hope this makes sense. Any help greatly appreciated. I'm having a blast with MVD.

 procedure AlternateRowColours;
var
   i,k,r,c: integer;
begin
     r := frmExpenseDS.TableGrid1.RowCount - 1;
     c := frmExpenseDS.TableGrid1.Columns.Count - 1;
     for i := 0 to r do
          begin
           if i mod 2 = 0 then
              begin
                for k := 0 to c do
                  begin
                    frmExpenseDS.TableGrid1.Cell[k,i].Color := $00F0F0F0;
                  end;
              end;
          end;
end;

Re: TableGrid Alternating Row Colour Procedure

Hello Clyde

It is true, with 20 tablegrids on 20 differents forms,

It is unnecessary to code 20 times the same snippet.
The idea would be to create a single procedure in which the name of the tablegrid concerned would be passed as a parameter.
The following code would allow this but with the current version of MVD (2.8), you can not work on the canvas of the grid.

This code

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
const  RowHeight = 18;    // Value to change if necessary
begin
  DBGrid1.Canvas.Font.Color := clBlack;
  if Odd(rect.top div RowHeight) then
    DBGrid1.canvas.Brush.Color := clAqua
  else
    DBGrid1.Canvas.Brush.Color := clWhite;
  DBGrid1.canvas.TextRect(rect,rect.Left,rect.top,table.fields[DataCol].AsString);
end;

Perhaps Dmitry would have another solution?

JB

Re: TableGrid Alternating Row Colour Procedure

clyde

Example:

procedure Form1_TableGrid1_OnChange (Sender: string);
begin
    TableGrid_OnChange(Form1.TableGrid1);
end;

procedure Form1_GridEmployees_OnChange (Sender: string);
begin
    TableGrid_OnChange(Form1.GridEmployees);
end;


procedure TableGrid_OnChange (Sender: TdbStringGridEx);
var
   i,k: integer;
begin
    for i := 0 to Sender.RowCount - 1 do
    begin
        for k := 0 to Sender.Columns.Count - 1 do
        begin
            Sender.Cell[k,i].Color := $00F0F0F0;
        end;
    end;
end;
Dmitry.

Re: TableGrid Alternating Row Colour Procedure

Hello Dmitry, hello Clyde

Good idea to create a procedure apart to reuse it without write it again and again

I note this code in my snippets collector

Thanks

JB

Re: TableGrid Alternating Row Colour Procedure

@Dmitry - That was exactly what I was looking for - thank you! I have one question though, is TdbStringGridEx referenced anywhere in the documentation or the functions reference in the code editor? I couldn't find it. Anyway, I've added an example project file to show how I'm using it. Hope it can help some other forum members.

Post's attachments

Attachment icon Expenses.zip 342.35 kb, 651 downloads since 2016-11-17 

Re: TableGrid Alternating Row Colour Procedure

clyde wrote:

@Dmitry - That was exactly what I was looking for - thank you! I have one question though, is TdbStringGridEx referenced anywhere in the documentation or the functions reference in the code editor? I couldn't find it. Anyway, I've added an example project file to show how I'm using it. Hope it can help some other forum members.

For grid Iam using component TNextGrid 5 from bergsoft.net.
Please download attached help file for the component, look section "GridView"

Post's attachments

Attachment icon Help.chm 159.05 kb, 693 downloads since 2016-11-18 

Dmitry.

Re: TableGrid Alternating Row Colour Procedure

@Dmitry - Thanks for the Bergsoft info, but the help file is empty - just a TreeView on the left with no content displayed when clicking on one of the TreeView nodes sad

Re: TableGrid Alternating Row Colour Procedure

Hello Clyde, hello Dmitry

I can read this chm fie.
My PC is running under Windows 10 which don't want read this kind of file.
Therefore, you (and I) can read with SUMATRA PDF from 1.9.
Dedicated to PDF file it is able to read CHM file.

This help file about Bergsoft is very interesting.
So, if the need arises, we can ask Dmitry to implement some functions

JB

Re: TableGrid Alternating Row Colour Procedure

@Dmitry & @Jean - I'm running Windows 10 too. Did a search and came up with the following that works for me.
Uncheck the box "Always ask...when opening this file..." when you first open the file. Afterwards, the popup no longer appears and you can read the CHM file's contents. After doing this the file opens as expected - no need to rely on 3rd party software, but thanks, @Jean for posting what worked for you smile.