Drawing rows with alternate colors in TsDBGrid

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #37673
    HeDiBo
    Participant

    I would like to draw a TsDBGrid with alternate row colors.

    I tried this:

    Code:

    type
    TsMyDBGrid = class(acDBGrid.TsDBGrid)
    // Makes the Row property available in this module
    end{class};
    .
    .
    .

    procedure TfrSettings.DbGridGetCellParams( Sender: TObject;
    Field: TField;
    AFont: TFont;
    var Background: TColor;
    State: TGridDrawState;
    StateEx: TGridDrawStateEx );
    begin
    if gdSelected in State then Exit; // No background color change in selected cells
    if gdFixed in State then Exit; // or fixed cells
    if sSkinManager.Active then
    if (TsMyDBGrid(DbGrid).Row mod 2) = 0
    then Background := sSkinManager.Palette[pcEditBG_EvenRow]
    else Background := sSkinManager.Palette[pcEditBG_OddRow]
    end;

    but that failed miserably (some rows got the even row color, some rows the odd row color but there was no regularity in it.

    Do you know how to solve this?

    #57377
    Support
    Keymaster

    You can't link a color of row with tow number here (with Row property).

    Try the DataSource.DataSet.RecNo for this purpose.

    http://www.swissdelphicenter.ch/en/showcode.php?id=2305

    #57421
    HeDiBo
    Participant
    'Support' wrote:

    You can't link a color of row with tow number here (with Row property).

    Try the DataSource.DataSet.RecNo for this purpose.

    http://www.swissdelp…ode.php?id=2305

    Unfortunately not all datasets have a working RecNo property. Especially SQL based datasets do not have one or have only a simulated one that takes enormous amounts of time.

    Wouldn't it be possible for you to build this feature into TsDBGrid:

    Property AlternateColors: Boolean which defaults to False for compatibility with previous versions.

    Event OnGetAlternateColor(OddRow: Boolean; CurrColor: TColor): TColor. (Only needed for customized alternate colors)

    Considering the number of web pages with explanations / questions on how to do this in TDBGrid that must be a welcomed addition to AC.

    The alternate color logic only has to be based on the physical row, not on content or record number of the underlying dataset (first displayed row has the odd color, second displayed row has the even color, etc.):

    Code:
    RowNo := Rect.Top div (Rect.Bottom – Rect.Top);
    OddRow := Odd(RowNo);

    Would you? Could you? bravo.gif

    #57434
    Support
    Keymaster

    I think, calculated fields may be used for such purposes in some databases.

    I have researched standard grid controls and don't see any info about current row number, unfortunately.

    So, I don't know a good solution which exists in standard grid already, all existing solutions are based on information received from DB, seems.

    #57441
    HeDiBo
    Participant
    'Support' wrote:

    I think, calculated fields may be used for such purposes in some databases.

    I have researched standard grid controls and don't see any info about current row number, unfortunately.

    So, I don't know a good solution which exists in standard grid already, all existing solutions are based on information received from DB, seems.

    Have another look at the code I gave.

    The background color is not related to the dataset contents, just to the row of the cell being painted. That is given by the Rect of the cell to be drawn by means of

    Code:
    RowNo := Rect.Top div (Rect.Bottom – Rect.Top);

    It is not based on a record number or what have you.

    Could you have another look at this, please happy.gif

    #57446
    Support
    Keymaster

    We will receive a number of the Visible row in this case.

    But we haven't information about of amount of records before first visible row. If grid is scrolled then rows must be moved too.

    First visible row will have constant color always, but we need to change colors of rows when they are scrolled up or down.

    I hope, you understand what I mean.

    #57451
    HeDiBo
    Participant
    'Support' wrote:

    We will receive a number of the Visible row in this case.

    But we haven't information about of amount of records before first visible row. If grid is scrolled then rows must be moved too.

    I can understand, if you think it is important that a certain background color stays with the same record. But I consider these alternate colors as nothing more than line paper. They are a reading aid to aid the user in keeping on the same line, nothing more.

    Keeping the background color with the record is not possible anyway: inserting a record in the visible part of a grid will affect the background color for all rows following it.

    So I think if grid is scrolled, the background color of the rows does not have to scroll too. In fact, if you have a look at the TJvDBGrid implementation in the attached project. It uses this principle and it looks completely OK:

    [attachment=8578:acJvGridBkgr.zip]

    #57463
    Support
    Keymaster

    I see what you mean, it looks Ok. But I can't recompile the demo because sources are wrong.

    I will think about implementing of such coloring.

    #57470
    HeDiBo
    Participant
    'Support' wrote:

    But I can't recompile the demo because sources are wrong.

    I made a mistake, including the wrong source.

    This is the right source and I included the source for TJvDBGrid (it's public domain)

    [attachment=8582:acJvGridBkgr.zip]

    #57492
    Support
    Keymaster

    This solution is not bad, I will try to implement it soon, thank you.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.