TStringGrid、TGrid 都是从 TCustomGrid 继承; 区别有:
1、它们的列对象分别是: TStringColumn、TColumn;
2、TStringGrid 比 TGrid 多出了 Cells[] 属性.
因为 TGrid 没有 Cells[] 属性, 暂时不方便使用; 我尝试取其当前单元值时竟然用了这样的代码:
(Grid1.Columns[Grid1.ColumnIndex].CellControlByRow(Grid1.Selected) as TTextCell).Text
TStringGrid 测试:
{ 设计时放好 StringGrid1, 运行时填充数据 } procedure TForm1.FormCreate(Sender: TObject); var i,c,r: Integer; begin StringGrid1.AlternatingRowBackground := True; StringGrid1.UseSmallScrollBars := True; for i := 0 to 5 do //从设计时添加列比这方便 begin with TStringColumn.Create(Self) do begin Parent := StringGrid1; Width := StringGrid1.ClientWidth / 6; end; end; StringGrid1.RowCount := 20; for c := 0 to StringGrid1.ColumnCount - 1 do for r := 0 to StringGrid1.RowCount - 1 do StringGrid1.Cells[c, r] := Format('%d,%d', [c, r]); end; { 取当前单元值 } procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(StringGrid1.Cells[StringGrid1.ColumnIndex, StringGrid1.Selected]); end;
成员概览:
{ TCustomGrid } public constructor Create(...); override; // destructor Destroy; override; // function ColumnByIndex(...): TColumn; //根据索引获取列对象 function ColumnByPoint(...): TColumn; //根据位置获取列对象 function RowByPoint(...): Integer; //根据位置获取行号 procedure AddObject(...); override; // property TopRow: Integer ...; //获取可见的首行的行号 property VisibleRows: Integer ...; //获取可见的行总数 property ColumnCount: Integer ...; //列数(也是只读) property ColumnIndex: Integer ...; //获取或设置列索引 property Columns[Index: Integer]: TColumn ...; //以数组索引的方式获取列对象 property RowCount: Integer ...; //行数(可读写) property Selected: Integer ...; //当前行号 property OnGetValue: TOnGetValue ...; //取值时 property OnSetValue: TOnSetValue ...; //赋值时 published property StyleLookup; // property AlternatingRowBackground: Boolean ...; //是否使用交替背景; 默认 False property CanFocus default True; // property DisableFocusEffect default True; //是否取消焦点特效 property RowHeight: Single ...; //行高 property ShowSelectedCell: Boolean ...; //是否呈现单元选择效果; 默认 True property ShowVertLines: Boolean ...; //是否显示竖格线 property ShowHorzLines: Boolean ...; //是否显示横格线 property ShowHeader: Boolean ...; //是否显示表格头 property ReadOnly: Boolean ...; //是否只读; 默认 False property TabOrder; // property OnEdititingDone: TOnEdititingDone ...; //输入时 end; { TGrid } TGrid = class(TCustomGrid) published property RowCount; // property OnGetValue; // property OnSetValue; // end; { TStringGrid } public property Cells[ACol, ARow: Integer]: string ...; // published property RowCount; // end;