Database Grid Control DBGrid of Delphi
To thoroughly customize the drawing of a DBGrid control, you have to set its DefaultDrawing property to False and handle its OnDrawColumnCell event.
The DBGrid control in this example, which is connected to the commonly used BIOLIFE table of the DBDEMOS database, has the following properties:
object DBGrid1: TDBGrid Align = alClient DataSource = DataSource1 DefaultDrawing = False Font.Height = -16 Font.name = 'MS Sans Serif' Font.Style = [fsBold] TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] OnDrawColumnCell = DBGridDrawColumnCell end
MEMO Field
For the memo we can simply implement the memo field's OnGetText and OnSetText events. In fact, the grid will even allow editing on a memo field IF its OnSetText event is not nil or null.
( You have to double click on the Table component or Query component and at the Field Editor select the TableNotes field. Under it's event tab you'll be able to insert these codes. )
procedure TForm1.TableNotesGetText (Sender: TField; var Text: String; DisplayText: Boolean) begin Text := Trim(Sender.AsString); end; procedure TForm1.TableNotesSetText (Sender: TField; const Text: String); begin Sender.AsString := Text; end; Multiple Selection toggle th dgMultiSelect element of the options property of the grid. Once you've selected this option, a user can keep the CTRL key pressed and click with the mouse to select multiple rows of the grid, with the effect you can see below and as follows: procedure TForm1.Button1Click (Sender: TObject); var I: Integer; BookmarkList: TBookmarkList; Bookmark: TBookmarkStr; begin // store the current position Bookmark := Table1.Bookmark; try // empty the list box ListBox1.Items.Clear;
// get the selected rows of the grid BookmarkList := DBGrid1.SelectedRows; for I := 0 to BookmarkList.count - 1 do begin // for each, move the table to that record Table1.Bookmark := BookmarkList[I]; // add the name field to the listbox ListBox1.Items.Add (Table1.FieldByName ('Name').AsString); end; finally // go back to the initial record Table1.Bookmark := Bookmark; end; end; | Color Me Red The OnDrawColumnCell event handler is called once for every cell of the grid and has several parameters, including the rectangle corresponding to the cell, the index of the column we have to draw, the column itself and the status of the cell. How can we set the color of specific cell to red ? see code below: procedure TForm1.DBGridDrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin // red font color if length > 100 if (COlumn.Field = Table1Lengthcm) and (Table1Lengthcm.AsInteger > 100) then DBGrid1.Canvas.Font.Color := clRed; // default drawing DBGrid1.DefaultDrawDataCell (Rect, Column.Field, State); end; Note: Table1Lengthcm is a field column in DBGrid1 Image For the image, the simplest approach is to create a temporary TBitmap object, assign the graphics field to it, and paint the bitmap to the Canvas of the grid. var Bmp: TBitmap; OutRect: TRect; BmpWidth: Integer; begin // default output rectangle OutRect := Rect; If Column.Field = TableCommon_Name then begin // draw the image Bmp := TBitmap.create; try Bmp.Assign(TableGraphic); BmpWidth := (Rect.Bottom - Rect.Top) * 2; OutRect.Right := Rect.Left + BmpWidth; DBGrid1.Canvas.StretchDraw (OutRect, Bmp); finally Bmp.Free; end; // reset output rectangle, // leaving space for the // graphic OutRect := Rect; OutRect.Left := OutRect.Left + BmpWidth; end;
As you can see in the code above, the program shows the image in a small rectangle on the left of the grid cell and then changes the output rectangle to the remaining area before activating the default drawing. to be inserted at the DBGridDrawColumnCell procedure, also. |

