how to keep track of selected rows with dgRowSelect = False - delphi

When dgRowSelect = False how can i detect the selected row within the OnDrawColumnCell method?
Not the selected cell, but the row that contains the selected cell.

The code below seems to work. The TDBGrid still keeps SelectedRows updated (even though it doesn't draw with them without dgRowSelect enabled), so you can still access them in your drawing code. (You do still need to enable dgMultiSelect, even though dgRowSelect is not needed.)
The code lets the grid do all of the drawing, just setting the Canvas.Brush.Color on the selected rows. The supplied color will be overridden by the drawing code for a single cell if the state of that cell happens to be gdSelected.
I've set the color of the selected rows to clFuchsia, and left just the selected cell the default color for clarity (the grid is ugly with clFuchsia selected rows, but it works to demonstrate):
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState);
var
Selected: Boolean;
Grid: TDBGrid;
begin
Grid := TDBGrid(Sender);
if not (gdSelected in State) then
begin
Selected := Grid.SelectedRows.CurrentRowSelected;
if Selected then
Grid.Canvas.Brush.Color := clFuchsia;
end;
Grid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
Sample results of above, with the first and third rows selected:
You can, of course, just use the usual selected color of clHighLight; I found it to be confusing, though, because the current cell of an unselected row matched the color of the selected rows exactly. If they're directly next to each other, it was visually annoying.

Related

DBGrid save cell color after OnCellClick (delphi, lazarus)

Sorry for my English.
I have some table (from datasource->mssql server->views), and i need to delete/hide/assign text color= white/any other things for clicked cells in DBGrid.
Like: i clicked cells->cells font=white(or clicked .text:=''/etc..);save;clicked next cells; repeat.
I try do like this:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdSelected in State
then begin
with DBGrid1.Canvas do
begin
Brush.Color:=clWhite;
Font.Color:=clWhite;
FillRect(Rect);
end;
end;
end;
But it works for only 1 cell: when i click next cells the color becomes standart (like in another cells in DBGrid) for the previous cell. How i can save cell color for all clicked cells? //Complicated by the fact that I do not know much about Delphi Thanks!
I believe it is because you are using if gdselected in state. Only one cell at a time is selected so only that cell is drawn using your blanking code.
You need to set a property when a cell is clicked (say set tag = 1, or something like that), then check that tag along the lines of "if Sender.Tag > 0 then . I believe the sender is the cell itself and so something like TControl( Sender ).tag or some other appropriate cast will get you what you want.

change colors of some rows in dbgrid delphi 5

I have a problem, I have an TStringList object with some numbers and his status in some kind of json text I created, like: {'8987436','Sin documentar.', '0','1'}, {...},...
This file can contain a lot of groups of data. This works really fine, this information I displayed in a DBGrid, only the numbers.
The problem is that when I try to change the color of the rows, only draw the color of the last number added to the dbgrid, and I need that each row with some type of number draw in yellow, other kind in red, other kind in green and all the other let it white. The other problem is that when I click on one row it refresh the dbgrid and paint it again in white.
I created this procedure:
procedure TEmbarqueGeneracionEscaneoFRM.DBValidasDrawColumnCell(
Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
inherited;
if Pinta then
begin
with (Sender As TDBGrid).Canvas do
begin
brush.Color:=GridColor;
FillRect(Rect);
TextOut(Rect.Left, Rect.Top, Column.Field.AsString);
end;
TDBGrid(sender).DefaultDrawColumnCell(Rect, Datacol, Column, State);
end;
Pinta := false;
end;
Where Pinta is a variable that tell if the number was painted, and GridColor is a TColor variable with the color that will be drawn.
Did you have any idea?
Regards

FireMonkey XE5 - Livebinding - TGrid - Cell text aligment

I have a Firemonkey Desktop Application for Windows. I have a TGrid which I populate through a visual livebinding TBindSourceDB component. I want some columns to have text aligned to the right, as they are numerical values. I have tried through:
onPainting event
getting the TTextCell control by number of ColumnxRow
typecasting it and setting TextAlignt property to the right
None of those measures align the text to the right. I have tried to set it at runtime, unsuccessfully though, getting the TStyledControl and assigning procedures to the onApplyStyleLookup of the TTextCell.
Any ideas on it? The App runs, but nothing happens. The cell texts are still left aligned.
Use the OnDrawColumnCell Event.
For columns containing text cells, the text layout information for each individual Column is assigned from the TextSettings property of the grid. However, the assignment is performed prior to the event firing.
The best and simplest way is to just directly access the layout for a specific column via a class helper before any drawing takes place.
Set the DefaultDrawing property of the grid to False and paste the following code:
interface
type
TColumnHelper = class helper for FMX.Grid.TColumn
function getTextLayout: TTextLayout;
end;
implementation
{ TColumnHelper }
function TColumnHelper.getTextLayout: TTextLayout;
begin
Result := Self.FDrawLayout;
end;
{ OnDrawColumnCell }
procedure GridDrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
begin
{ change text layout info prior to default drawing }
if Column.Header = 'Numerical Column' then
Column.getTextLayout.HorizontalAlign := TTextAlign.Trailing
else
Column.getTextLayout.HorizontalAlign := TGrid(Sender).TextSettings.HorzAlign;
{ perform default drawing }
TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
end;

How to highlight a DBGrid cell?

I'm trying to programatically highlight the current cell in a TDBGrid descendant. If I do DBGrid.SetFocus, I get the combo box arrow below, which isn't sufficiently highlighted for me.
EDIT:
I'm already doing DBGrid.SelectedField := DataSource.FieldByName('Name');
To bring the user's attention more to the region in question, I set:
DBGrid.Columns[x].Title.Font.Style := [fsbold, fsunderline];
And I set a timer that after five seconds does:
DBGrid.Columns[x].Title.Font.Style := [];
What's weird is that after the time goes off, the cell becomes blue (as shown below.) That's the highlight I wanted in the first place. But I don't know enough about grids to know how to get that directly.
My question: how to I get a grid cell highlighted as in the blue example below? I've never done anything like this before, so I'm a bit lost. Is this an InPlaceEditor function?
I'm using a descendant of TDBGrid, so I'm not sure if the behavior I'm seeing is intrinsic to TDBGrid, or just in the descendant (in which case I know my question can't be answered here. )
I've been using the following (D2007) using the DBGrid: OnDrawColumnCell event.
procedure TForm1.DBGridDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
//Make the column blue if the title is bold
if (fsBold in Column.Title.Font.Style) then
TDBGrid(Sender).Canvas.Brush.Color := $00fff0e1;
//Set the selected row to white/bold text on blue background
if (gdSelected in State) then
begin
TDBGrid(Sender).Canvas.Brush.Color := clHighlight;
TDBGrid(Sender).Canvas.Font.Style := Font.Style + [fsBold];
TDBGrid(Sender).Canvas.Font.Color := clHighlightText;
end;
//Update the grid
TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

Delphi TDBGrid How to change selected color when style is gdsGradient

I'm just trying to use delphi XE, before that I've been a big fan of Delphi7.
I see the new dbgrid allows to use themed and gradient styles.
I'm using gradient and set rowselect, it has a property for gradient-start and -end for the column header.
But where is the property to set the selected color ?
It's strange because the color doesn't match, selected color is always a blue gradient.
I can do it with customdraw, I just want to know if there is anyway to change it without custom drawing.
The selected color comes from the OS.
There it's coded as clHighlight.
You cannot change it as such, but you can subclass the dbgrid and override the DrawCell method.
Or even easier add a onDrawCell eventhandler.
procedure TForm1.DBGrid1DrawCell(Sender: TObject, const Rect: TRect; Field: TField; State: TGridDrawState);
var
index: Integer;
begin
if not(gdSelected in State) then DefaultDrawCell(Rect, Field, State)
else begin
index := ARow * DBGrid1.ColCount + ACol;
DBGrid1.Canvas.Brush.Color := clYellow; <<-- some color
DBGrid1.Canvas.FillRect(Rect);
if (gdFocused in State) then begin
DBGrid1.Canvas.DrawFocusRect(Rect);
end;
ImageList1.Draw(DBGrid1.Canvas,Rect.Left,Rect.Top,index, True);
end;

Resources