Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Базы данных и репортинг > DBGrid


Автор: MacTep 29.11.2004, 23:46
Как можно в ячейке с годом рождения (тип Date) дописать что-то типа такого: 1990 год. Т.е. не просто число года, а еще и рядом слово год. Только не нужно использовать стринговое поле!

Автор: Dimich 30.11.2004, 10:01
Код
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
 const Rect: TRect; DataCol: Integer; Column: TColumn;
 State: TGridDrawState);
begin
 with CRDBGrid1.Canvas do
   if Column.FieldName = 'MYDATE' then
     begin
       FillRect (Rect);
       TextOut (Rect.Left+1,
                Rect.Top+1,
                IntToStr (YearOf (Column.Field.AsDateTime))
                  + ' год');
     end;
end;

Автор: gray_k 30.11.2004, 16:47
Для этого предусмотрено с тандартное событие OnGetText у поля набора данных.

Автор: MacTep 30.11.2004, 21:35
У какого компонента это все есть. Я про это событие читал, но так и не смог отызкать его во вкладках Events хотя бы какого-то компонента. Подскажите, пожалуйста!

Автор: Pakshin A. S. 30.11.2004, 22:58
Народ, стоять.....
Юзаем SQL!
Код

SELECT Name, BirthDay || " год" FROM Dates;

Имеем: после дня рождения добавлено " год":
11.11.1111 год

Попробуй, может прокатит... smile

P. S.
Может и так:
Код

SELECT Name, BirthDay || ' год' FROM Dates;

В учебнике во всяком случае так написано... smile

Автор: Dimich 1.12.2004, 08:16
Ув. Pakshin A. S.!
MacTep просил:
Цитата
Только не нужно использовать стринговое поле!


А про OnGetText правильно сказано, только вот у меня что-то не получается с ним работать.....

В Help'e написано так:
Цитата
Occurs when the DisplayText or Text properties of the field are referenced.

type TFieldGetTextEvent = procedure(Sender: TField; var Text: String; DisplayText: Boolean) of object;
property OnGetText: TFieldGetTextEvent;

Description

Write an OnGetText handler to perform custom processing for the DisplayText and Text properties. Use OnGetText to format the Value of the field differently when it must be edited from the format used when simply displaying the value, or use OnGetText to display something other than the field’s value when it is displayed.

The Sender parameter is the field component. The Text parameter returns the formatted text that is used by the DisplayText or Text property. The DisplayText parameter indicates whether the text will be used for display purposes only, or whether the string will be used for editing.

If there is no OnGetText handler, the value of the DisplayText and Text properties is simply the value of the AsString property.

Киньте плиз кусок кода, если не затруднит, чтобы наглядно разобраться...

Автор: Pakshin A. S. 1.12.2004, 19:34
smile Кто сказал, что юзано стринговое поле?! BirthDate - формат типа Date...

Автор: MacTep 1.12.2004, 23:30
Так мне никто про OnGetText и не рассказал!

Автор: Dimich 2.12.2004, 14:44
Код
type
 TForm1 = class(TForm)
  ...........
   procedure MyGetText(Sender: TField; var Text: String; DisplayText: Boolean);
 private
   { Private declarations }
 public
   { Public declarations }
 end;
  ...........
procedure TForm1.MyGetText(Sender: TField; var Text: String; DisplayText: Boolean);
begin
if Sender.DataType = ftString then
  Text := AnsiUpperCase(Sender.AsString);
end;

  ...........
 Query1.Open;
 DBGrid1.Fields [1].OnGetText := MyGetText;

Вот такой корявый примерчик юзания OnGetText.......

Кстати, Pakshin A. S.:
Цитата
Кто сказал, что юзано стринговое поле?! BirthDate - формат типа Date...
Вы не правы! Юзано как раз стринговое поле! BirthDay - формат типа Date, а вот BirthDay || ' год' as MySirthDay это уже стринговое!

Автор: Pakshin A. S. 2.12.2004, 16:17
Ну и что? Но результат, то...

Автор: Dimich 2.12.2004, 19:30
Нет, я ничего против не имею, просто вопрос был
Цитата
Только не нужно использовать стринговое поле!

Хотя конечно я бы сделал селект, как предложили Вы! В самом деле, ну и что, что поле стринговое?

Автор: MacTep 3.12.2004, 23:12
Спасибо всем, очень полезные советы!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)