Delphi DBgrid Wrap Word Content - delphi

Is it possible to Word Wrap DBgrid Cell (height) in Delphi (xe) ?
I have a dbgrid with multiple columns with a fixed Column width and some columuns has up to 100char and I need them to fit the appropriate Cell without changing the Column width.
Any clue how to do it ?
Thanks.

You'll need to make it ownerdraw, and in the OnDrawCell event put the code to fill in the cells:
procedure TForm4.Grid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
Grid1.Canvas.TextRect(Rect, Rect.Left+1, Rect.Top+1, WrapText(Grid1.Cells[ACol,ARow], 40));
end;

Related

How to get values of cells in tmsfmxlivegrid hidden columns?

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.

DBAdvGrid RowHeights - alter all rows except 1

I'm using the following code in the CustomCellDraw event of DBAdvGrid(TMS) to increase row height.
procedure TForm1.DBAdvGrid1CustomCellDraw(Sender: TObject; Canvas: TCanvas;
ACol, ARow: Integer; AState: TGridDrawState; ARect: TRect; Printing: Boolean);
begin
DBAdvGrid1.RowHeights[ARow]:=120;
end;
How do I make it avoid increasing row 0, which is the 1st row in the Grid, containing column names/headers? - I'd like that row to remain untouched while all the rest should get resized via the above code. Basically it should ignore row index 0 and start from row index 1
It would be like this:
procedure TForm1.DBAdvGrid1CustomCellDraw(Sender: TObject; Canvas: TCanvas;
ACol, ARow: Integer; AState: TGridDrawState; ARect: TRect; Printing: Boolean);
begin
if ARow > 0 then
DBAdvGrid1.RowHeights[ARow] := 120;
end;
But do not modify row heights from a drawing event. Such event is triggered frequently, and is used exclusively for content painting, not for adjusting content size. What's worse, if you e.g. allowed row sizing and the user would try to setup row height, it would in turn trigger that event where you would change the height back, so you'd be fighting with the user.
Content sizing should be done earlier, as this example shows in the OnCustomCellSize event.
But for your aim I think it's enough to set DefaultRowHeight and FixedRowHeight properties with no additional code.

delphi dbgrid drawing cell image black background

Problem using image from a TImage list to draw a glyph on to a data cell in DBGrid:
I am putting a bmp image of a "checkmark" in place of the text "Done" in a particular data cell. It works, but there is always black color in the parts of the cell not covered by the image. I have tried enlarging the pixel size of the bmp image to match the cell size, but it always seems to resize the image for me. Using Delphi 10.2, was not problem in D7?
Have tried many combos of setting background colors, pen and brush colors, etc. Here is a simple example of one code attempt:
procedure TFUpRepWS.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with Column do begin
if ((FieldName = 'Done') and (Field.AsString = 'x')) then begin
//below shows black outside of check mark image in the cell
ImageList1.Draw(DBGrid1.Canvas,Rect.Left,Rect.Top,0)
end
else DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;
Do the default cell painting DefaultDrawColumnCell always. That will ensure the cell will look like the others. Then draw the image. Try this:
procedure TFUpRepWS.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with Column do
begin
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if ((FieldName = 'Done') and (Field.AsString = 'x')) then
ImageList1.Draw(DBGrid1.Canvas, Rect.Left, Rect.Top, 0);
end;
end;
I guess that what you described happens because there is no code that paints the cell background.

Draw TBitmaps in DrawGrid in Delphi

I have an 8 x 16 DrawGrid in Delphi XE5 that I would like to randomly fill with nine images I've stored in C:\Users\Sean Ewing\Documents\My Documents\Delphi Tutorials\Other\Math-O-Sphere\Win32\Debug\img. I'm currently trying to get one image to load to make sure I'm doing it correctly. Here is the code I've used to do this:
procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
spherePlus: TBitmap;
begin
spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) + '\img\Sphere +1.bmp');
grdPlayField.Canvas.Draw(0, 0, spherePlus);
end;
The code compiles fine, and based on what I've read in the Embarcadero wiki this is correct, but I get an error at runtime when it's time to load the DrawGgrid. Where did I go wrong?
You need to first create the bitmap before you can use it:
procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
spherePlus: TBitmap;
begin
spherePlus := TBitmap.Create;
try
spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) +
'\img\Sphere +1.bmp');
grdPlayField.Canvas.Draw(0, 0, spherePlus);
finally
spherePlus.Free;
end;
end;
The other thing you should be aware of is that the Rect parameter you receive in the event is the area that needs to be painted, so you'll want to use Canvas.StretchDraw and pass it that rectangle. It won't help with the current issue, but you'll need it when you move to the next step. You can identify the exact cell that's being drawn with the ACol and ARow parameters, so you can use that information to load a specific image for a column, for instance, or to output text for a column or row.
// Load specific image for the cell passed in ACol and ARow,
// and then draw it to the appropriate area using the Rect provided.
grdPlayField.Canvas.StretchDraw(Rect, spherePlus);

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;

Resources