在StringGrid中如何使“编号”和“房台名称”分行显示? ( 积分: 27 )

  • 主题发起人 主题发起人 坚持信念
  • 开始时间 开始时间

坚持信念

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Test;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Grids, DB, ADODB, BaseGrid, AdvGrid;

type
TCellColor = class
Color: TColor;
Text: string;
end; // 单元格颜色类

TfrmTest = class(TForm)
StringGrid1: TStringGrid;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
procedure InitGrid(AStringGrid: TStringGrid);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;

var
frmTest: TfrmTest;

implementation

{$R *.dfm}

procedure TfrmTest.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
CellColor: TCellColor;
begin
CellColor := TCellColor(StringGrid1.Objects[ACol, ARow]);
if Assigned(CellColor) then begin
with TStringGrid(Sender).Canvas do begin
Brush.Color := CellColor.Color;
FillRect(Rect);
TextRect(Rect, Rect.Left + 2, Rect.Top + 2, CellColor.Text);
end;
end;


// DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);
end;

constructor TfrmTest.Create(AOwner: TComponent);
begin
inherited;
InitGrid(StringGrid1);
end;

destructor TfrmTest.Destroy;
var
I, J: Integer;
begin
for I := 0 to StringGrid1.RowCount do begin
for J := 0 to StringGrid1.ColCount do begin
TCellColor(StringGrid1.Objects[J, I]).Free;
end;
end;
inherited;
end;

procedure TfrmTest.InitGrid(AStringGrid: TStringGrid);
var
i,j:integer;
CellColor: TCellColor;
begin
i:=0;
j:=0;
while not ADOQuery1.Eof do
begin
AStringGrid.Cells[i,j]:=ADOQuery1.FieldByName('编号').AsString+ADOQuery1.FieldByName('房台名称').AsString;
CellColor := TCellColor.Create;
if ADOQuery1['状态']='空闲' then
begin
CellColor.Color := clred;
end;
if ADOQuery1['状态']='营业' then
begin
CellColor.Color := clblue;
end;

CellColor.Text := AStringGrid.Cells[i, j];
AStringGrid.Objects[i, j] := CellColor;
if i<4 then
inc(i)
else begin
i:=0;
inc(j);
end;
ADOQuery1.Next;
end;
StringGrid1.RowCount := j+1;
StringGrid1.ColCount := 5;
end;

end.
 
unit Test;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Grids, DB, ADODB, BaseGrid, AdvGrid;

type
TCellColor = class
Color: TColor;
Text: string;
end; // 单元格颜色类

TfrmTest = class(TForm)
StringGrid1: TStringGrid;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
procedure InitGrid(AStringGrid: TStringGrid);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;

var
frmTest: TfrmTest;

implementation

{$R *.dfm}

procedure TfrmTest.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
CellColor: TCellColor;
begin
CellColor := TCellColor(StringGrid1.Objects[ACol, ARow]);
if Assigned(CellColor) then begin
with TStringGrid(Sender).Canvas do begin
Brush.Color := CellColor.Color;
FillRect(Rect);
TextRect(Rect, Rect.Left + 2, Rect.Top + 2, CellColor.Text);
end;
end;


// DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);
end;

constructor TfrmTest.Create(AOwner: TComponent);
begin
inherited;
InitGrid(StringGrid1);
end;

destructor TfrmTest.Destroy;
var
I, J: Integer;
begin
for I := 0 to StringGrid1.RowCount do begin
for J := 0 to StringGrid1.ColCount do begin
TCellColor(StringGrid1.Objects[J, I]).Free;
end;
end;
inherited;
end;

procedure TfrmTest.InitGrid(AStringGrid: TStringGrid);
var
i,j:integer;
CellColor: TCellColor;
begin
i:=0;
j:=0;
while not ADOQuery1.Eof do
begin
AStringGrid.Cells[i,j]:=ADOQuery1.FieldByName('编号').AsString+ADOQuery1.FieldByName('房台名称').AsString;
CellColor := TCellColor.Create;
if ADOQuery1['状态']='空闲' then
begin
CellColor.Color := clred;
end;
if ADOQuery1['状态']='营业' then
begin
CellColor.Color := clblue;
end;

CellColor.Text := AStringGrid.Cells[i, j];
AStringGrid.Objects[i, j] := CellColor;
if i<4 then
inc(i)
else begin
i:=0;
inc(j);
end;
ADOQuery1.Next;
end;
StringGrid1.RowCount := j+1;
StringGrid1.ColCount := 5;
end;

end.
 
把这句改成:
AStringGrid.Cells[i,j]:=ADOQuery1.FieldByName('编号').AsString + #13 + #10 + ADOQuery1.FieldByName('房台名称').AsString;


procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
TheGrid : TStringGrid;
A : String;
begin
TheGrid := Sender as TStringGrid;
TheGrid.Canvas.FillRect(Rect);
A := TheGrid.Cells[ACol,ARow];
DrawText(TheGrid.Canvas.Handle, PChar(A), StrLen(PChar(A)), Rect, DT_WORDBREAK);
end;

这个就可以了,你是作收银的吧,我也是,我们应该算是同行了:)
 
那怎样获取所选择的格子第一行内容?
 
如果房台显示

0001
长江

怎么把0001找出来呢?

联系QQ:57959940
 
s := stringgrid1.Cells[i,j];

index := pos(#13#10, s);

showmessage(copy(s, 1, index-1));

showmessage(copy(s, index+2, Length(s)-index-1));
 
接受答案了.
 
后退
顶部