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;