Страница 1 из 1

Как раскрасить строку tDB*Grid в зависимости от поля?

СообщениеДобавлено: 19.12.2013 14:57:58
t-ea
Есть таблица с данными, например:
Код: Выделить всё
Наименование Начало       Конец        Сделано
----------------------------------------------
Заседание    01.01.2013   02.02.2013   Да
Собрание     07.01.2013   07.01.2013   Нет
Праздник     01.02.2013   31.12.2013


В tDBStringGrid из этих колонок выводится только поле 'Наименование'.
Как сделать, чтобы строка, с «Заседание», была перечёркнута, а фон «Собрание», например, был бы красный?

Я так подозреваю, что надо что-то делать с CellInfo.DataPo^. в OnBeforeDrawCell, но вот как?

Re: Как раскрасить строку tDB*Grid в зависимости от поля?

СообщениеДобавлено: 19.12.2013 16:17:21
hovadur
t-ea писал(а):Я так подозреваю, что надо что-то делать с CellInfo.DataPo^. в OnBeforeDrawCell, но вот как?

Не в OnBeforeDrawCell как в delphi, а OnPrepareCanvas, как написано здесь http://wiki.lazarus.freepascal.org/Grid ... ng_process
Пример кода:
Код: Выделить всё
procedure TfrmMus.DBGridPrepareCanvas(sender: TObject; DataCol: Integer;
  Column: TColumn; AState: TGridDrawState);
var
  Grid: TCustomDBGrid;
  D: TDataSet;
begin
  Grid := TCustomDBGrid(Sender);
  if (Column.FieldName = 'INUSL') then begin
    D := Column.Field.DataSet;
    if D.FieldByName('mex').AsInteger <> 1 then begin
      Grid.Canvas.Brush.Color := clRed;;
      Grid.Canvas.Font.Color := clMenuText;
    end;
  end;
end;

Re: Как раскрасить строку tDB*Grid в зависимости от поля?

СообщениеДобавлено: 19.12.2013 17:24:01
mse
In order to set the row background color by a DB field:
- define the colors in t*grid.rowcolors.
- create an integer field which holds the color index, possibly it is a fkCalculated or fkInternalCalc field.
- select the color field in tdb*grid.datalink.fieldname_color.
- disable options1 co1_rowcolor in columns which should not change the background color.
An example is in mseuniverse/samples/db/gridcolorlines.
https://gitorious.org/mseuniverse/mseuniverse
It is also possible to select the font and merged, readonly and selected state of rows by a data field.

If you want to use onbeforedrawcell set cellinfo.color depending on the field value. Working with datapo is maybe a little bit dangerous, possibly the dataset cursor should be placed to the current row in the drawing events, I'll have a look.
The grid data can be accessed in tdbwidgetgrid by <editwidget>[cellinfo.cell.row], in tdbstringgrid by <column>[cellinfo.cell.row].

Posted after 1 hour 10 minutes 32 seconds:
mse писал(а):possibly the dataset cursor should be placed to the current row in the drawing events, I'll have a look.

Done, git master f7b4a55483e56e0f25cfc02864e6d0a3ca9650b0.