起因
网友在QQ群里问Text字段的显示问题。
俺回答:百度了一下 方法有 很多很多 。然后给他百度了6种方案。然后告诉他找个顺眼的用。
可是,对方感觉都顺眼。俺就新写了一个 。
代码
创建测试数据
begin
ADODataSet1.DisableControls;
colcnt:=5;
rowcnt:=10;
for i:=1 to colcnt do
ADODataSet1.FieldDefs.Add('TEXT'+inttostr(i),ftMemo);
ADODataSet1.CreateDataSet();
for j:=1 to rowcnt do
begin
ADODataSet1.Append();
for i:=1 to colcnt do
ADODataSet1.Fieldbyname('TEXT'+inttostr(i)).AsString:='啥啥';
ADODataSet1.Post();
end;
ADODataSet1.First;
for i:=1 to colcnt do
ADODataSet1.Fieldbyname('TEXT'+inttostr(i)).OnGetText:= OnGetText;
ADODataSet1.EnableControls;
end;
并设置OnGetText事件
ADODataSet1.Fieldbyname('TEXT'+inttostr(i)).OnGetText:= OnGetText;
OnGetText事件
procedure TForm1.OnGetText(Sender: TField; var Text: string;
DisplayText: Boolean);
begin
Text:=Sender.AsString;
DisplayText:=true;
end;
全部代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
ADODataSet1: TADODataSet;
DBGrid1: TDBGrid;
procedure FormCreate(Sender: TObject);
private
procedure OnGetText(Sender: TField; var Text: string;
DisplayText: Boolean) ;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
colcnt,i:integer;
rowcnt,j:integer;
begin
ADODataSet1.DisableControls;
colcnt:=5;
rowcnt:=10;
for i:=1 to colcnt do
ADODataSet1.FieldDefs.Add('TEXT'+inttostr(i),ftMemo);
ADODataSet1.CreateDataSet();
for j:=1 to rowcnt do
begin
ADODataSet1.Append();
for i:=1 to colcnt do
ADODataSet1.Fieldbyname('TEXT'+inttostr(i)).AsString:='啥啥';
ADODataSet1.Post();
end;
ADODataSet1.First;
for i:=1 to colcnt do
ADODataSet1.Fieldbyname('TEXT'+inttostr(i)).OnGetText:= OnGetText;
ADODataSet1.EnableControls;
end;
procedure TForm1.OnGetText(Sender: TField; var Text: string;
DisplayText: Boolean);
begin
Text:=Sender.AsString;
DisplayText:=true;
end;
end.