TRzDBGrid AltRowShading Colours

Home Forums Konopka Signature VCL Controls (formerly Raize Components) TRzDBGrid AltRowShading Colours

Viewing 2 reply threads
  • Author
    Posts
    • #1765
      Barry Wood
      Participant

        Hi,

         

        When I use the AltRowShading with the Alt colour as $00EBF5EB (a light blue) all works fine until I select a row of that colour, when it changes to a dark blue. This is a problem as I then can hardly see the text (see attached). Is there a setting for this (either text or background colour)?

        TRzDBGridColours

      • #1767
        Ray Konopka
        Keymaster

          Hi Barry,

          Unfortunately, this is another case where Embarcadero needs to allocate some resources to keep the KSVC updated. The display problem you are seeing is a result of the DrawingStyle property that was added to the TCommonGrid and thus TDBGrid, which is the ancestor of the TRzDBGrid. The default value of DrawingStyle is gdsThemed. If you change the property to gdsGradient or gdsClassic you will see that the highlighted rows are displayed correctly. If you use gdsGradient, the gradient colors of the fixed cells is controlled by the GradientStartColor and GradientEndColor properties. Set them to the same color and there is essentially no gradient. The selected row is shown as a gradient based on the system highlighted color (i.e. clHighlight).

          If changing the DrawingStyle will not work, then you have two options. The first is to draw the cells yourself. Set the DefaultDrawing property to False and then write an event handler for the OnDrawColumnCell event. For example,

          uses
            Vcl.Themes,
            RzCommon;
          
          procedure TForm16.RzDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
            State: TGridDrawState);
          var
            Grid: TRzDBGrid;
            BrushColor, FontColor: TColor;
          begin
            Grid := Sender as TRzDBGrid;
          
            // Set the default colors
            BrushColor := clWindow;
            FontColor := clWindowText;
            if gdSelected in State then
            begin
              BrushColor := $00FFD199;
            end;
          
            if Grid.AltRowShading then
            begin
              if Column.Field.DataSet.RecNo mod 2 = 1 then
              begin
                if not ( gdSelected in State ) then
                begin
                  BrushColor := Grid.AltRowShadingColor;
                  FontColor := clWindowText;
                end;
              end;
            end;
          
            Grid.Canvas.Brush.Color := BrushColor;
            Grid.Canvas.Font.Color := FontColor;
            Grid.DefaultDrawColumnCell( Rect, DataCol, Column, State );
          end;

          The event handler takes care of setting the background (i.e. brush) and font colors and then uses the built-in DefaultDrawColumnCell method to actually do the drawing. The solution works, but it is a bit simplistic. For instance, the highlight color is hard coded. There are functions in the Style Services to get the various colors of a grid, but unfortunately, the TCommonGrid does NOT use them. Ugh. Instead, the base grid “draws” the grid cells from the active style. Unfortunately, do the same inside this event handler is not really an option.

          The second option is to modify the TRzDBGrid source code. Specifically, the TRzDBGrid.DrawColumnCell method. The fragment below contains the top portion of the method with comments indicating the lines to change.

          procedure TRzDBGrid.DrawColumnCell( const Rect: TRect; DataCol: Integer;
                                              Column: TColumn; State: TGridDrawState );
          var
            Success: Boolean;
            {$IFDEF VCL140_OR_HIGHER}
            S: string;
            {$ENDIF}
          begin
            // Set cell background color the right way before calling user event
            if FAltRowShading then
            begin
              if ( FAltRowShadingFixed and ( ( DataLink.ActiveRecord mod 2 ) = 0 ) ) or                // Scrolling background
                 ( ( not FAltRowShadingFixed ) and ( ( Column.Field.DataSet.RecNo mod 2 ) = 1 ) ) then // Fixed background
              begin
                if HighlightCell( DataCol, DataLink.ActiveRecord, Column.Field.DisplayText, State ) then
                begin
                  if DrawingStyle = gdsThemed then       // Add
                    Canvas.Brush.Color := clWindow       // Add
                  else                                   // Add
                    Canvas.Brush.Color := clHighlight;   // Indent
                end
                else
                . . . 
          

          After making the change and saving the file, you can either add the RC6\Source directory to your project’s search path, or you can shutdown Delphi and using the !Build_RC6.cmd file in the Source directory to rebuild the components and packages. Either way, when you run the app, the highlighted bar will display correctly when DrawingStyle is set to gdsThemed.

          One note about the above code modification. It is certainly odd that setting the Canvas.Brush.Color to clWindow results in the correct highlight color being used. I discovered this rather by accident. Initially I tried using the Style Services functions I mentioned earlier to get the cell color for the grid, but all of the grid colors kept coming up clWindow (clWhite on my machine). However, with the color set to clWindow, the correct highlight color was displayed. But, if you use a different color, the highlight color changes. I believe this is due to the default style used for the grid uses a semi transparent color for the highlight color. When the base color is set to clWindow (i.e. clWhite) the highlight color matches exactly with the non-alt-shaded rows. A different base color results in a different highlight color.

          Hope this helps,
          Ray

        • #1768
          Barry Wood
          Participant

            Ray,

            That certainly helped. Just amazed at the depth of detail in the reply, many thanks. The first ‘fix’ you mentioned has worked a treat on my dbgrid.

            I do have another dbgrid showing the same issues but on this one I do need to use OnDrawColumnCell to colour specific cells depending on their values, so I’ll explore your other solutions in more details.

            Delphi is a wonderful tool to enable ‘digging into the code’ to fix issues like this. It’s just a shame that Embarcadero (perhaps understandably) don’t put the amount of resource required into fixing the ‘smaller’ issues in their attempts to capture bigger markets. I feel they rely on forums/support groups (such as this one) to help smooth over problems they don’t have time to fix.

             

            Again, thanks for the swift reply.

        Viewing 2 reply threads
        • You must be logged in to reply to this topic.