Delphi的stringGrid,ValueListEditor等表格相关 我想做类似MSSQL创建表差不多的功能,就是允许输入一行,一旦输入某一行后,自动添加下一个空白行。同时支持选中行删除。不想用第三方控件,有没有什么比较好的方法。MSSQL截图如下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不想用三方控件,就自己扩展,给你推荐一个比较好的控件:TDBGridEh 简单写了一个程序,供参考在最后一行按下箭头增加一行,双击删除当前行一些代码是网上贴过来的,格式较乱,但测试一下(XP+D6)基本符合你的要求.unit gridtest;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls;type TForm1 = class(TForm) StringGrid1: TStringGrid; procedure FormCreate(Sender: TObject); procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure StringGrid1Click(Sender: TObject); procedure StringGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure StringGrid1DblClick(Sender: TObject); private { Private declarations } public { Public declarations } fcheck,fnocheck:tbitmap; procedure init; procedure addgrid; procedure DeleteRow(ARow: Longint); end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.init;vari:SmallInt;bmp:TBitmap;beginFCheck:= TBitmap.Create;FNoCheck:= TBitmap.Create;bmp:= TBitmap.create;trybmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES ));With FNoCheck Do Beginwidth := bmp.width div 4;height := bmp.height div 3;canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect );End;With FCheck Do Beginwidth := bmp.width div 4;height := bmp.height div 3;canvas.copyrect(canvas.cliprect,bmp.canvas,rect( width, 0, 2*width, height ));End;finallybmp.free;end;StringGrid1.Cells[1,0]:='列名';StringGrid1.Cells[2,0]:='数据类型';StringGrid1.Cells[3,0]:='充许空';end;procedure TForm1.addgrid;var i: Integer; CurrentRow:integer;begin //先增加一行 StringGrid1.RowCount := StringGrid1.RowCount + 1; StringGrid1.Rows[StringGrid1.RowCount - 1].Clear; //移形换位,把最后一行跟你指定的行换位置! CurrentRow := StringGrid1.Row+1; for i := StringGrid1.RowCount - 1 downto CurrentRow + 1 do begin StringGrid1.Rows[i].Assign(StringGrid1.Rows[i - 1]); end; StringGrid1.Rows[CurrentRow].Clear; StringGrid1.Row := CurrentRow; SendMessage(StringGrid1.Handle, EM_SCROLLCARET, 0, 0);//多送你一个方法:滚动到插入行的位置end;procedure TForm1.DeleteRow(ARow:integer);var i,mrow:integer; begin if arow<2 then //保留一行 exit; with stringgrid1 do begin mrow:=RowCount; for i:=arow to mrow-2 do Rows[i].Assign(Rows[i+1]); rowcount:=rowcount-1; end; end; procedure TForm1.FormCreate(Sender: TObject);begin Init;end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);beginif (acol=3) and (arow>=1) thenbeginif not (gdFixed in State) thenwith TStringGrid(Sender).Canvas dobeginbrush.Color:=clWindow;FillRect(Rect);if StringGrid1.Cells[ACol,ARow]='yes' thenDraw( (rect.right + rect.left - FCheck.width) div 2,(rect.bottom + rect.top - FCheck.height) div 2,FCheck )elseDraw( (rect.right + rect.left - FCheck.width) div 2,(rect.bottom + rect.top - FCheck.height) div 2,FNoCheck );end;end;end;procedure TForm1.StringGrid1Click(Sender: TObject);beginif (StringGrid1.col=3) and (StringGrid1.row>=1) thenbeginStringGrid1.Options:=StringGrid1.Options-[goEditing] ;if StringGrid1.Cells[StringGrid1.col,StringGrid1.row]='yes' thenStringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='no'elseStringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='yes';end elseStringGrid1.Options:=StringGrid1.Options+[goEditing] ;end;procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if (key=vk_down) and ( StringGrid1.Row=StringGrid1.RowCount-1) then addgrid;end;procedure TForm1.StringGrid1DblClick(Sender: TObject);begin DeleteRow( StringGrid1.Row);end;end. 用dbgrid就可以,只不过要处理几个按键:回车(若是记录尾则新增)、下箭头(若是记录尾则新增)、删除(ctrl+del键,删除当前记录) 可用dbgrid处理strings不是很麻烦嘛。 我不是连数据库,只是单纯处理TSTRINGLIST 这个很好实现,输入完后直接stringgrid1.rowcount:=stringgrid1.rowcount+1; 小侄子出世,各位闲暇之余不妨来给起个名字,谢谢!!第一次发贴请名 如何将编译到EXE中的资源文件释放并生成一个文件? 关于DBCOMBOX的问题 动态生成的Button排序问题 快来看 急急急!!!各位,如何处理Image(详见内容) 关于DirectShow播放DVD时选择字幕问题 怎样每次都接着上次打印的位置打印 重载 默认的session的 CheckDatabaseAlias 方法时 为什么要出错? #=====>是高手都进来讨论这个问题,来者都有分,快来一起讨论吧 时间段判断的问题,应该对高手不难,帮忙看看 delphi 获取活动窗口名?
在最后一行按下箭头增加一行,双击删除当前行
一些代码是网上贴过来的,格式较乱,但测试一下(XP+D6)基本符合你的要求.unit gridtest;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1Click(Sender: TObject);
procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure StringGrid1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
fcheck,fnocheck:tbitmap;
procedure init;
procedure addgrid;
procedure DeleteRow(ARow: Longint);
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.init;
var
i:SmallInt;
bmp:TBitmap;
begin
FCheck:= TBitmap.Create;
FNoCheck:= TBitmap.Create;
bmp:= TBitmap.create;
try
bmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES ));
With FNoCheck Do Begin
width := bmp.width div 4;
height := bmp.height div 3;
canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect );
End;
With FCheck Do Begin
width := bmp.width div 4;
height := bmp.height div 3;
canvas.copyrect(
canvas.cliprect,
bmp.canvas,
rect( width, 0, 2*width, height ));
End;
finally
bmp.free;
end;StringGrid1.Cells[1,0]:='列名';
StringGrid1.Cells[2,0]:='数据类型';
StringGrid1.Cells[3,0]:='充许空';end;procedure TForm1.addgrid;
var
i: Integer;
CurrentRow:integer;
begin
//先增加一行
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Rows[StringGrid1.RowCount - 1].Clear;
//移形换位,把最后一行跟你指定的行换位置!
CurrentRow := StringGrid1.Row+1;
for i := StringGrid1.RowCount - 1 downto CurrentRow + 1 do
begin
StringGrid1.Rows[i].Assign(StringGrid1.Rows[i - 1]);
end;
StringGrid1.Rows[CurrentRow].Clear;
StringGrid1.Row := CurrentRow;
SendMessage(StringGrid1.Handle, EM_SCROLLCARET, 0, 0);//多送你一个方法:滚动到插入行的位置
end;
procedure TForm1.DeleteRow(ARow:integer);
var
i,mrow:integer;
begin
if arow<2 then //保留一行
exit;
with stringgrid1 do
begin
mrow:=RowCount;
for i:=arow to mrow-2 do
Rows[i].Assign(Rows[i+1]);
rowcount:=rowcount-1;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Init;
end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (acol=3) and (arow>=1) then
begin
if not (gdFixed in State) then
with TStringGrid(Sender).Canvas do
begin
brush.Color:=clWindow;
FillRect(Rect);
if StringGrid1.Cells[ACol,ARow]='yes' then
Draw( (rect.right + rect.left - FCheck.width) div 2,
(rect.bottom + rect.top - FCheck.height) div 2,
FCheck )
else
Draw( (rect.right + rect.left - FCheck.width) div 2,
(rect.bottom + rect.top - FCheck.height) div 2,
FNoCheck );
end;
end;
end;procedure TForm1.StringGrid1Click(Sender: TObject);
begin
if (StringGrid1.col=3) and (StringGrid1.row>=1) then
begin
StringGrid1.Options:=StringGrid1.Options-[goEditing] ;
if StringGrid1.Cells[StringGrid1.col,StringGrid1.row]='yes' then
StringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='no'
else
StringGrid1.Cells[StringGrid1.col,StringGrid1.row]:='yes';
end else
StringGrid1.Options:=StringGrid1.Options+[goEditing] ;
end;
procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=vk_down) and ( StringGrid1.Row=StringGrid1.RowCount-1) then
addgrid;end;procedure TForm1.StringGrid1DblClick(Sender: TObject);
begin
DeleteRow( StringGrid1.Row);
end;end.