可以用StringGrid,在cell里面用ComboBox、edit来编辑数据,
在Form中放一个StringGrid,一个ComboBox和一个edit,
程序大抵如下,还需你进一步完善,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
ComboBox1:TComboBox;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Edit1Change(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
x,y:integer;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Visible:=False;
ComboBox1.Visible:=False;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
x:=aCol;
y:=aRow;
if (gdFocused in State) then
begin
if aCol = 1 then
begin
Edit1.Text := StringGrid1.Cells[x,y];
Edit1.Left := Rect.Left + StringGrid1.Left + 2;
Edit1.Top := Rect.Top + StringGrid1.top + 2;
Edit1.Width := Rect.Right - Rect.Left;
Edit1.Height := Rect.Bottom - Rect.Top;
Edit1.Visible := True;
end;
if aCol = 0 then
begin
ComboBox1.Text := StringGrid1.Cells[x,y];
ComboBox1.Left := Rect.Left + StringGrid1.Left + 2;
ComboBox1.Top := Rect.Top + StringGrid1.top + 2;
ComboBox1.Width := Rect.Right - Rect.Left;
ComboBox1.Height := Rect.Bottom - Rect.Top;
ComboBox1.Visible := True;
end;
end else
begin
Edit1.Visible:=False;
ComboBox1.Visible:=False;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
//判断数据有效性
StringGrid1.Cells[x,y]:=Edit1.Text;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
StringGrid1.Cells[x,y]:=ComboBox1.Text;
end;
end.