How to get values of cells in tmsfmxlivegrid hidden columns? - delphi

i want to change text color cells in specific column basing on values of other cells in other column using the GetCellLayout event like this.
procedure TForm1.TMSFMXLiveGrid1GetCellLayout(Sender: TObject;
ACol, ARow: Integer; ALayout: TTMSFMXGridCellLayout; ACellState: TCellState);
begin
if ACol = 1 then
if TMSFMXLiveGrid1.Cells[5, ARow] <> '' then
ALayout.FontFill.Color := TAlphaColorRec.Red;
end;
the problem is that when the column 5 is not visible in the screen area(you must scroll to see it) the color still black instead of red, because the column 5 is not loaded;
NB:the grid is binded to a dataset.

Related

Delphi TListView Trouble

I have a TListView on my form. I add some columns in it depending on the input like so:
MyItem := StringListView.Columns.Add;
MyItem.Caption := IntToStr(i);
MyItem.Width := -2;
Afterwards I use the onData event to populate the ListView like this:
procedure TMatrixDictViewerFrame.StringListViewData(Sender: TObject;
Item: TListItem);
var
ItemCaption: string;
ItemText: string;`
begin
ItemCaption := Format('[%d]', [Item.Index]);
ItemText := FItems[Item.Index];
Item.Caption := ItemCaption;
Item.SubItems.Add(ItemText);
end;
It works fine since in the First column I have the Itemcaptions and in the second column I get the Itemtexts. What I couldnt figure out tho is how to populate the ListView depending on the data I get.
For example: I have a matrix A which is a 3x3 Matrix and I want its elements to be shown in this ListView so the first row shows the first 3 row elements, the second row shows the second row three elements and so on. Questions: how can I access the third column? How can I populate the view according to the Index I have (i,j)?
Best regards
The Index property of the list item tells you the row. You are expected to populate the entire row. Like this:
procedure TMatrixDictViewerFrame.StringListViewData(Sender: TObject; Item: TListItem);
begin
// A is a 3x3 matrix, that you obtain by means we don't know
Item.Caption := FloatToStr(A[Item.Index, 0]);
Item.SubItems.Add(FloatToStr(A[Item.Index, 1]));
Item.SubItems.Add(FloatToStr(A[Item.Index, 2]));
end;

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

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;

What is the rect of a clicked Title on a TDBGrid in Delphi?

I'd like to identify the screen coordinates of the Title cell which was clicked on the TDBGrid event TitleClick(Column: TColumn).
I can use the ColWidths property (exposed via a TDBGrid = class(DBGrids.TDBGrid) type declaration) but I am having difficulty in determining if the columns have been re-ordered by the user, combined with horizontal scrolling of the TDBGrid. I'd also like to keep track of where this Column is during subsequent moves and resizings, noting also that it's possible this column can be scrolled off the grid.
I've spent a long while on this problem and I am rather good at Delphi so this is not an idle question.
Using the trick in How do I get screen coordinates of the DBGrid cell, I wrote:
type
...
THackedGrid = class(TDBGrid);
...
implementation
...
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
currRow : Integer;
rect : TRect;
begin
currRow := THackedGrid(DBGrid1).Row;
rect := THackedGrid(DBGrid1).CellRect(Column.Index+1,currRow);
end;
Is this what you want? The coordinate in rect is relative to the grid.
I started working on a very similar grid yesterday at work. I am overlaying a control on the grid fixed row as you mentioned, activating it on a right click. This is what I doing so far, then setting the filter on my dataset. I have issues however when using multiselect on the combo. I would be very interested in seeing what you have accomplished since the last post.
procedure Tf_well.dbWellGridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var cell : TGridCoord;
begin
if Button = mbRight then
begin
Cell := dbWellGrid.MouseCoord(X, Y);
// showmessage(dbWellGrid.Columns[Cell.X-1].DisplayName);
case Cell.X-1 of
0: begin
fieldComboWellName.Visible:=True;
fieldComboWellName.DroppedDown:=True;
fieldComboWellName.SetFocus;
end;

With TMS TDBAdvGrid, how to color lines with different colors depending on cell values?

All is in the title.
How can we also make an officehint customisable for each row. Mean when mousemove on a row, display the information of this record (from a db query).
Thanks
You can color individual cells using the CellProperties property of the grid. You can use this to color an entire row:
var
RowIndex: Integer;
ColIndex: Integer;
with MyDBAdvGrid do
begin
// you choose the row index; you may want to iterate all rows to
// color each of them
RowIndex := 2;
// now iterate all (non-fixed, visible) cells in the row and color each cell
for ColIndex := FixedCols to ColCount - 1 do
begin
CellProperties[ColIndex, RowIndex].BrushColor := clYellow;
CellProperties[ColIndex, RowIndex].FontColor := clGreen;
end;
end;
To fill your office hint with record data I would suggest updating it when the user moves the mouse. Use the MouseToCell function to get row and column under the mouse, then use MyDBAdvGrid.AllCells[ColIndex, RowIndex] to access the cell content.
An Alternative to Heinrich answer is to use the OnGetCellColor event.
This can be use like so:
procedure TDBAdvGrid.DBGridGetCellColor(Sender: TObject; ARow,
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
begin
if (your condition) then ABrush.Color := clRed;
end;
Similarly for the hint:
procedure TDBAdvGrid.DBGridGridHint(Sender: TObject; ARow, ACol: Integer;
var hintstr: String);
begin
hintstr := 'your hint text';
end;

Resources