- This topic has 9 replies, 2 voices, and was last updated 6 years, 9 months ago by Support.
-
AuthorPosts
-
December 11, 2017 at 3:32 pm #37673HeDiBoParticipant
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?
December 18, 2017 at 1:24 pm #57377SupportKeymasterYou can't link a color of row with tow number here (with Row property).
Try the DataSource.DataSet.RecNo for this purpose.
December 27, 2017 at 4:59 pm #57421HeDiBoParticipant'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.
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?
January 4, 2018 at 10:34 am #57434SupportKeymasterI 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.
January 4, 2018 at 5:14 pm #57441HeDiBoParticipant'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
January 5, 2018 at 8:48 am #57446SupportKeymasterWe 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.
January 5, 2018 at 12:48 pm #57451HeDiBoParticipant'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]
January 10, 2018 at 6:03 pm #57463SupportKeymasterI 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.
January 10, 2018 at 7:17 pm #57470HeDiBoParticipant'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]
January 25, 2018 at 8:18 am #57492SupportKeymasterThis solution is not bad, I will try to implement it soon, thank you.
-
AuthorPosts
- You must be logged in to reply to this topic.