Как заменить данные в столбце компонента TDBGrid
Falk0ner, вс, 06/07/2008 - 15:34.
Нередко в колонке DBGrid нужно вывести не реальное значение, хранящееся в поле соответствующей таблицы, а другие данные, соответствующие имеющимся (например, символьную строку вместо ее числового кода). В этом случае также используется метод TextOut свойства Canvas компонента TDBGrid:
Соответствующий код для Delphi имеет вид:
procedure TForm1.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName = 'VenueNo' then
with DBGrid1.Canvas do
begin
Brush.Color := clWhite;
FillRect(Rect);
if Table.FieldByName('VanueNo').Value = 1 then
begin
Font.Color := clRed;
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('our vanue'),
Rect. + 2, 'our vanue');
end
else
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('other vanue'),
Rect. + 2, 'other vanue');
end;
end;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName = 'VenueNo' then
with DBGrid1.Canvas do
begin
Brush.Color := clWhite;
FillRect(Rect);
if Table.FieldByName('VanueNo').Value = 1 then
begin
Font.Color := clRed;
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('our vanue'),
Rect. + 2, 'our vanue');
end
else
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('other vanue'),
Rect. + 2, 'other vanue');
end;
end;
Еще один пример – использование значков из шрифтов Windings или Webdings в качестве подставляемой строки.
Соответствующий код для Delphi имеет вид:
procedure TForm1.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName = 'VenueNo' then
with DBGrid1.Canvas do
begin
Brush.Color := clWhite;
FillRect(Rect);
Font.name := 'Windings';
Font.Size := -14;
if Table.FieldByName('VanueNo').Value = 1 then
begin
Font.Color := clRed;
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('J'),
Rect. + 2, 'J');
end
else
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('F'),
Rect. + 2, 'F');
end;
end;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName = 'VenueNo' then
with DBGrid1.Canvas do
begin
Brush.Color := clWhite;
FillRect(Rect);
Font.name := 'Windings';
Font.Size := -14;
if Table.FieldByName('VanueNo').Value = 1 then
begin
Font.Color := clRed;
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('J'),
Rect. + 2, 'J');
end
else
TextOut(Rect.Right - 2 - DBGrid1.Canvas.TextWidth('F'),
Rect. + 2, 'F');
end;
end;
Взято с http://delphiworld.narod.ru
Отправить комментарий