以下是在WIN7 64 ,delphi xe 10.3 以下成功运行:
运行界面:
代码
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Rtti,
FMX.Grid.Style, FMX.StdCtrls, FMX.Grid, FMX.Controls.Presentation,
FMX.ScrollBox, FMX.Memo
//,System.Types
,FMX.Header //THeaderItem
// ,System.UITypes //[TFontStyle.fsBold]
;
type
TForm1 = class(TForm)
stringgrid1: TStringGrid;
A: TStringColumn;
B: TStringColumn;
C: TStringColumn;
D: TStringColumn;
E: TStringColumn;
Panel1: TPanel;
StyleBook1: TStyleBook;
CornerButton1: TCornerButton;
CornerButton2: TCornerButton;
CornerButton3: TCornerButton;
procedure FormCreate(Sender: TObject);
procedure AppendClick(Sender: TObject);
procedure DelClick(Sender: TObject);
procedure InsertClick(Sender: TObject);
procedure stringgrid1DrawColumnHeader(Sender: TObject;
const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF);
procedure stringgrid1HeaderClick(Column: TColumn);
procedure CornerButton2Click(Sender: TObject);
procedure CornerButton1Click(Sender: TObject);
procedure CornerButton3Click(Sender: TObject);
private
{ Private declarations }
procedure addRow(const row:integer);
procedure delRow(const row :integer);
procedure insertRow(const row:integer;val:array of Variant);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.AppendClick(Sender: TObject);
var
temprow :integer;
begin
for temprow := 0 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells [0,temprow].IsEmpty then
begin
break; //此处不能用EXIT 哟
end;
end;
addRow(temprow);
end;
procedure TForm1.addRow(const row:integer); //在末尾追加
var
col,Selrow:integer;
begin
selRow:=row;
for selRow:= selRow to selRow do
for col := 0 to stringgrid1.ColumnCount-1 do
begin
stringgrid1.Cells[col,selRow]:=selRow.ToString ;
end;
end;
procedure TForm1.delRow(const row :integer);
var
col,Selrow:integer;
temprow,rowcount:integer;
begin
for temprow := 0 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells [0,temprow].IsEmpty then //第一列为空,则表明后面全部为空行。
begin
rowcount:=temprow ;
break; //此处不能用EXIT 哟
end;
end;
Selrow:=row;
for Selrow := Selrow to (rowcount) do //当前行开始,把后面的数据覆盖当前行;
for col := 0 to stringgrid1.ColumnCount-1 do
begin
stringgrid1.Cells[col,Selrow]:= stringgrid1.Cells[col,Selrow+1] ; //用后面的数据覆盖当前行;
end
end;
procedure TForm1.insertRow(const row:integer;val:array of Variant);
var
col,Selrow:integer;
c,temprow ,rowcount:integer;
beGIN
//row 是选中行号
for temprow := 0 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells [0,temprow].IsEmpty then
begin
rowcount:=temprow ;
break; //此处不能用EXIT 哟
end;
end;
for c:=rowcount+1 downto row do
for col := 0 to stringgrid1.ColumnCount-1 do
begin
stringgrid1.Cells[col,c]:= stringgrid1.Cells[col,c-1] ;
end ; //上述是完成从当前行各自后移一行
//row 是选中行号
stringgrid1.Cells[0,row]:=val[0] ;
stringgrid1.Cells[1,row]:=val[1];
stringgrid1.Cells[2,row]:=val[2] ;
stringgrid1.Cells[3,row]:=val[3] ;
stringgrid1.Cells[4,row]:=val[4];
end;
procedure TForm1.stringgrid1DrawColumnHeader(Sender: TObject;
const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF);
var
Header: THeader;
HeaderItem: THeaderItem;
I: Integer;
begin
Header:= Theader((Sender as tstringgrid).FindStyleResource('header'));
if Assigned(Header) then
begin
for I := 0 to Header.Count - 1 do
begin
HeaderItem:=header.Items[i];
HeaderItem.Font.Size:=12;
HeaderItem.Font.Style:=[TFontStyle.fsBold ,TFontStyle.fsItalic, TFontStyle.fsUnderline];
HeaderItem.TextSettings.HorzAlign := TTextAlign.Center;
HeaderItem.StyledSettings := HeaderItem.StyledSettings - [TStyledSetting.Size, TStyledSetting.Style];
{
HeaderItem.Font.Style := [TFontStyle.fsBold];
property Style: TFontStyles read GetStyle write SetStyle stored False;
TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
TFontStyles = set of TFontStyle;
}
end; //for I := 0 to Header.Count - 1 do
Header.Height := 24;
end; //if Assigned(Header) then
end;
procedure TForm1.stringgrid1HeaderClick(Column: TColumn);
VAR
columnS:STRING;
begin
//SHOWMESSAGE(inttostr(chr(column.Name))) ;
// columnS:= column.Name;
CASE column.Index OF
0: //A
BEGIN
//按A列排序
END;
1: //B
BEGIN
//按B列排序
END;
2: //c
BEGIN
END;
3: //D
BEGIN
END;
4: //E
BEGIN
END;
END;
end;
procedure TForm1.CornerButton1Click(Sender: TObject);
var
temprow :integer;
begin
for temprow := 0 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells [0,temprow].IsEmpty then
begin
break; //此处不能用EXIT 哟
end;
end;
addRow(temprow);
end;
procedure TForm1.CornerButton2Click(Sender: TObject);
var
temprow:integer;
val :array[0..4] of Variant;
begin
if stringgrid1.Selected<>-1 then
begin
temprow:= stringgrid1.Selected;
val[0]:='AA';
val[1]:='BB';
val[2]:='CC';
val[3]:='DD';
val[4]:='EE';
insertRow(temprow,val);
end
else
begin
showmessage('你没有选择');
end;
end;
procedure TForm1.CornerButton3Click(Sender: TObject);
var
col,temprow ,rowcount:integer;
begin
if stringgrid1.Selected<>-1 then
begin
temprow:= stringgrid1.Selected;
delRow(temprow);
end
else
begin
showmessage('你没有选择');
end;
end;
procedure TForm1.DelClick(Sender: TObject);
var
col,temprow ,rowcount:integer;
begin
if stringgrid1.Selected<>-1 then
begin
temprow:= stringgrid1.Selected;
delRow(temprow);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
col,row:integer;
begin
row:=50; //添加50行测试数据
for row := 0 to (50-1) do
for col := 0 to stringgrid1.ColumnCount-1 do
begin
stringgrid1.Cells[col,row]:=row.ToString ;
end;
stringgrid1.Options:=[TGridOption.RowSelect,TGridOption.Header,TGridOption.Editing,TGridOption.HeaderClick]; //允许选择整行,显示标题 ,单击,
{
property Options: TGridOptions read GetOptions write SetOptions stored OptionsStored;
TGridOption = (AlternatingRowBackground, Editing, AlwaysShowEditor, ColumnResize, ColumnMove, ColLines, RowLines,
RowSelect, AlwaysShowSelection, Tabs, Header, HeaderClick, CancelEditingByDefault, AutoDisplacement);
}
//以下是单元格的对齐方式 ,没有针对各列的对齐方式,如针对某列的对齐方式,则需要重绘
stringgrid1.TextSettings.HorzAlign:=TTextAlign(0); ////文字横向对齐:0居中,1左,2右
stringgrid1.TextSettings.VertAlign:=TTextAlign(0);//文字纵向对齐:0居中,1左,2右
//以下加入样式
form1.StyleBook :=StyleBook1;//样式
StyleBook1.Resource.LoadFromFile('style\GoldenGraphite.Style');
end;
procedure TForm1.InsertClick(Sender: TObject);
var
temprow:integer;
val :array[0..4] of Variant;
begin
if stringgrid1.Selected<>-1 then
begin
temprow:= stringgrid1.Selected;
val[0]:='AA';
val[1]:='BB';
val[2]:='CC';
val[3]:='DD';
val[4]:='EE';
insertRow(temprow,val);
end
else
begin
showmessage('你没有选择');
end;
end;
end.
|
请发表评论