السلام عليكم ورحمة الله وبركاته
كنت أعمل علي قاعدة بيانات SQLite3 تحتوي علي حقول من نوع TEXT وعندما أردت أن أعرض هذه الحقول في جدول علي الفورم TBGrid فوجئت بأن الحقول TEXT لا تظهر بشكل صحيح ولكن يظهر مكانها (MEMO) كما يظهر بالصورة

بعد قليل من البحث وجدت حلا رائعا في منتديات lazarus وهو كود يقوم بالغاء الرسم التلقائي للـ Fields ورسمها عن طريق الكود لإظهار البيانات النصية , لتطبيق هذا الحل اتبع الخطوات التالية :
- حدد الـ TBGrid ثم من object inspector غير قيمة الخاصية DefaultDrawing من true إلي false .
- انتقل إلي لسان events في object inspector واختر الحدث onDrawColumnCell , سينقلك إلي محرر الكود أضف الكود التالي
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
// Draw memo text instead of (Memo), draw other text as usual.
// Note: normally, grid DefaultDrawing property must be off to avoid duplicate drawing
// However, we need it on against bug 21496
// This will result in duplicate drawing but should work.
{
Basically copied the existing DefaultDrawColumnCell procedure
but tested for memo first. If no memo, pass on to default procedure.
Maybe slower, more complicated, but it allows for changes in the
core Lazarus DefaultDrawColumnCell procedure.
Thanks to User137 & ludob on the Lazarus forum
}
var
//determine if we're going to override normal Lazarus draw routines
OverrideDraw: boolean;
OurDisplayString: string;
CurrentField: TField;
DataRow: Integer;
begin
OverrideDraw:=false;
// Make sure selected cells are highlighted
if (gdSelected in State) then
begin
(Sender as TDBGrid).Canvas.Brush.Color := clHighlight;
end
else
begin
(Sender as TDBGrid).Canvas.Brush.Color := (Sender as TDBGrid).Color;
end;
// Draw background in any case - thanks to ludob on the forum:
(Sender as TDBGrid).Canvas.FillRect(Rect);
//Foreground
try
CurrentField := Column.Field;
if CurrentField.DataType = ftMemo then
begin
OverrideDraw:=true;
end;
except
on E: exception do
begin
// We might have an inactive datalink or whatever,
// in that case, pass on our problems to the LCL
OverrideDraw:=false;
end;
end;
//Exception: fixed header should always be drawn like normal:
// this never gets picked up as OnDrawColumnCell apparently only deals with data cells!!!
if (gdFixed in State) then
begin
OverrideDraw:=false;
end;
if OverrideDraw=false then
begin
// Call normal procedure to handle drawing for us.
(Sender as TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end
else
begin
// Get to work displaying our memo contents
// Basically shamelessly ripped from
// DefaultDrawColumnCell
OurDisplayString := '';
if CurrentField<>nil then
begin
//DO display memo OurDisplayString is string to be displayed
try
OurDisplayString := CurrentField.AsString; //DisplayText will only show (Memo)
except
// Ignore errors; use empty string as specified above
end;
end;
//Actual foreground drawing, taken from Grids.DrawCellText coding:
(Sender as TDBGrid).Canvas.TextRect(Rect,Rect.Left,Rect.Top, OurDisplayString);
end;
end;اﻵن عند تشغيل التطبيق ستجد ان الحقول تظهر بشكل طبيعي

يمكنك متابعة النقاش حول هذه المشكلة هنا
الموضوع علي مدونتي هنا
