TDBGrid と cxGrid から sgcGrid への移行

sgcGrid は VCL 用のデータグリッド TsgcVCLGrid です。任意の TDataSet にバインドでき、グループ化、集計、フィルター、検索パネル、バンド、保存可能なレイアウトをすべてのエディションで利用できます。このキットでは、お使いの TDBGrid と cxGrid の各メンバーを sgcGrid の対応メンバーに結び付け、コンパイル済みデモのコードを示し、対応しない点も率直に挙げます。

2 つの出発点、1 つのグリッド

標準の TDBGrid を使っている Delphi VCL チーム、そして DevExpress cxGrid のサブスクリプションを見直しているチームが対象です。

TDBGrid から

標準の VCL TDBGrid はデータセットを表示して編集できますが、できるのはそこまでです。フィルター行、列フィルター、グループ化、フッターやグループの集計、検索パネル、バンド、ベストフィット、レイアウトの保存と読み込みはありません。これらはどれも独自コードかサードパーティ製グリッドが必要です。sgcGrid なら、すでにあるデータセットにバインドした 1 つのコントロールですべてを利用できます。

cxGrid から

DevExpress cxGrid (ExpressQuantumGrid) は幅広く成熟したグリッドです。主にグループ化、集計、フィルター、保存したレイアウトを備えたテーブルビューとして使っているチームなら、その範囲は完全なソースコード付きの sgcGrid で賄えます。グリッド以外にも多くを含むサブスクリプションをやめることができます。

現在のグリッドの費用です。2026-10-20 時点で、DevExpress VCL の価格表には 3 つのサブスクリプションがあり、いずれも ExpressQuantumGrid Control を含みます。最も安いのは Express QuantumPack で USD 999.99、VCL Subscription は USD 1,699.99 です。2025 年 9 月 18 日の Delphi-PRAXiS のスレッドでは、あるユーザーが cxGrid のサブスクリプションが USD 400 から USD 550 に上がったと報告し、別のメンバーは、新しい Express GridPack ライセンスはもう販売されないという DevExpress の告知を引用していました (有効なサブスクライバーは更新できます)。sgcGrid は開発者ごとのライセンスで €199 から提供され、完全なソースコードと 1 年間のアップデートが付属します。すべての sgcUI エディションにも含まれています。

任意の TDataSet にバインドする 1 つのコントロール

TsgcVCLGrid と、DataSet、Grid、KeyField を持つ TsgcGridSource_DataSet です。データセットのイベントによって、グリッドは常に同期されます。

すべてのエディション

FieldName で対応付ける設計時の列、ルックアップフィールド、セル、行、複数行の選択、行ヘッダー、カスタム描画、Ctrl+C、グループパネル、フッターとグループの集計、フィルター行、Excel 形式の列フィルター、検索パネル、バンド、ベストフィット、JSON によるレイアウトの保存と読み込み。

Registered エディションのみ

データセットへの双方向編集 (WriteBack)、カーソルの同期 (SyncCursor)、挿入、削除、貼り付け、元に戻す、カードビュー (ViewStyle)、非常に大きなテーブル向けのサーバーモード。体験版には評価用として含まれます。Community ビルドには含まれません。その場合でもデモはコンパイルでき、グリッドは読み取り専用で表示されます。

TDBGrid のメンバーと sgcGrid の対応

以下の sgcGrid の名前はすべて製品のソースコードにあり、デモで使っているものはそのままコンパイルできます。

TDBGrid sgcGrid エディション
TDBGridTsgcVCLGrid (ユニット sgcGrid_VCL)すべて
DataSourceTsgcGridSource_DataSet: DataSet、Grid、KeyField を設定し、Active := True にします (ユニット sgcGrid_Source_DataSet)すべて
Columns, TColumn.FieldNameColumns、Columns[i].FieldName。設計時の列は保持され、FieldName で対応付けられます。列がない場合は AutoColumns が作成しますすべて
Title.Caption, Width, VisibleColumns[i].Caption, Width, Visibleすべて
TColumn.ReadOnlyColumns[i].Editableすべて (編集は Registered)
TColumn.PickList、ルックアップフィールドのドロップダウンColumns[i].PickList。ルックアップフィールドでは、ソースが LookupDataSet から一覧を埋めますすべて (値の書き込みは Registered)
TField.DisplayFormatColumns[i].DisplayFormatすべて
ReadOnly, Options dgEditingTsgcVCLGrid.ReadOnly (既定は True) と TsgcGridSource_DataSet.WriteBackRegistered
dgRowSelectSelectionMode := gsmRow (指定しない場合は gsmCell)すべて
dgMultiSelect, SelectedRowsSelectionMode := gsmMultiRow; SelectedRows, SelectedRowCount, GetSelectedKeys, RowSelected[]すべて
dgIndicatorRowHeader := [grhIndicator] (grhNumbers、grhCheckBox も可)すべて
dgColLines, dgRowLinesGridLinesV, GridLinesHすべて
現在のレコードがグリッドに追従しますTsgcGridSource_DataSet.SyncCursorRegistered
OnDrawColumnCell, DefaultDrawColumnCellaDefaultDraw を使う OnCustomDrawCell。色だけを変えるなら OnGetCellStyle または FormatRulesすべて
OnTitleClickOnHeaderClickすべて
Ctrl+C (独自コード)組み込み済み (Ctrl+C、Ctrl+Ins)。テキストは SelectionToText から取得されますすべて
Insert キーと Ctrl+Del キーAllowInsert, AllowDelete, InsertRow, DeleteSelectedRows, OnBeforeDeleteRegistered

cxGrid のメンバーと sgcGrid の対応

cxGrid の名前は DevExpress の識別子であり、参考として示すだけです。

cxGrid sgcGrid エディション
TcxGridDBTableView, DataController.DataSourceTsgcVCLGrid と TsgcGridSource_DataSetすべて
TcxGridDBColumn.DataBinding.FieldNameColumns[i].FieldNameすべて
TcxGridDBColumn.GroupIndexColumns[i].GroupIndex; GroupBy(['Field']), ClearGrouping, GroupCountすべて
OptionsView.GroupByBoxShowGroupPanelすべて
DataController.Groups.FullExpand, FullCollapseExpandAll, CollapseAllすべて
OptionsView.FooterShowFooterすべて
DataController.Summary.FooterSummaryItems (skSum、skCount、skAverage、skMin、skMax)Columns[i].SummaryType (gsuSum、gsuCount、gsuAverage、gsuMin、gsuMax)、SummaryFormat。値は FooterSummary(aCol)、FooterSummaryText(aCol) から取得します。SummaryScope も参照してくださいすべて
DataController.Summary.DefaultGroupSummaryItemsGroupSummaries。グループ内では列の SummaryType を使いますすべて
FilterRow.VisibleShowFilterRow, Columns[i].FilterRowOperatorすべて
列フィルターのドロップダウンShowFilterButtons, ShowFilterPopup(aCol), ClearColumnFilter(aCol), ClearColumnFilters, OnFilterChangeすべて
FindPanel.DisplayModeShowFindPanel, FindText, FindNext, FindPrevious, FindOnlyMatchesすべて
TcxGridDBBandedTableView.Bands, Column.Position.BandIndexBands (Caption, ParentBand, Visible), Columns[i].Bandすべて
ApplyBestFitBestFitColumns, BestFitColumn(aCol), BestFitOnDblClick, BestFitSampleRowsすべて
StoreToIniFile, RestoreFromIniFileSaveLayoutToFile、LoadLayoutFromFile (JSON)。文字列やストリームとの入出力も可能で、AutoSaveLayout と LayoutStorageKey で自動化できますすべて
OptionsCustomize.ColumnMovingColumnMovingすべて
列のカスタマイズダイアログShowColumnChooserすべて
OnCustomDrawCellOnCustomDrawCellすべて
TcxGridDBCardView (2 つ目のビュー)同じグリッド上の ViewStyle := gvsCard と CardOptions (CaptionField、CardWidth、CardHeight、ShowFieldCaptions)Registered

コンパイル済みデモのコード

各コード抜粋は MigrateFromDBGrid デモからコピーしたもので、Delphi 7 と Delphi 13 (Win32 と Win64) でコンパイルできます。コメントには、置き換える TDBGrid または cxGrid のコードを記しています。

バインド: DataSource が TsgcGridSource_DataSet になります

移行前: DataSource := dsProducts を設定した TDBGrid。移行後: フォーム上のソースコンポーネントです。

frmMain.dfm
object SourceDB: TsgcGridSource_DataSet
  DataSet = cdsProducts
  Grid = GridDB
  KeyField = 'ID'
  Left = 280
  Top = 520
end

TDBGrid のオプションがプロパティになります

移行前: dgEditing、dgRowSelect、dgMultiSelect、dgIndicator を持つ Options。移行後:

frmMain.pas
procedure TFormMain.chkOptionsClick(Sender: TObject);
begin
  // TDBGrid.ReadOnly / Options dgEditing -> ReadOnly
{$IFDEF SGC_GRID_EDIT}
  GridDB.ReadOnly := not chkEditing.Checked;
{$ENDIF}
  // dgMultiSelect -> gsmMultiRow, dgRowSelect -> gsmRow, neither -> gsmCell
  if chkMultiSelect.Checked then
    GridDB.SelectionMode := gsmMultiRow
  else if chkRowSelect.Checked then
    GridDB.SelectionMode := gsmRow
  else
    GridDB.SelectionMode := gsmCell;
  // dgIndicator -> RowHeader [grhIndicator] (grhNumbers, grhCheckBox too)
  if chkIndicator.Checked then
    GridDB.RowHeader := [grhIndicator]
  else
    GridDB.RowHeader := [];
end;

OnDrawColumnCell が OnCustomDrawCell になります

移行前: Column.FieldName とフィールドの値を読む OnDrawColumnCell。移行後: aCol は Columns のインデックス、aRow は Data のビュー行です (CS_LOW_STOCK はデモの定数です)。

frmMain.pas
procedure TFormMain.GridDBCustomDrawCell(Sender: TObject; aCanvas: TsgcUICanvas;
  aRow, aCol, aLeft, aTop, aRight, aBottom: Integer;
  aState: TsgcGridCellDrawStates; var aDefaultDraw: Boolean);
begin
  // TDBGrid.OnDrawColumnCell: Column.FieldName and Field.AsInteger.
  // sgcGrid: aCol is the Columns index, aRow the view row of Data. The
  // background is painted already; aDefaultDraw False would skip the text
  if (aRow < 0) or (aRow >= GridDB.Data.ViewCount) or
    (GridDB.Columns[aCol].FieldName <> 'Qty') then
    Exit;
  if StrToIntDef(VarToStr(GridDB.Data.Cells[aRow, aCol]), 0) < CS_LOW_STOCK
  then
  begin
    // a red marker at the left edge of a low stock cell
    aCanvas.BeginPath;
    aCanvas.Rectangle(aLeft, aTop + 4, aLeft + 4, aBottom - 4);
    aCanvas.ClosePath;
    aCanvas.FillPath($FFDC2626);
  end;
end;

書き戻し、カーソルの同期、グループ化

移行前: TDBGrid の dgEditing、および cxGrid の GroupByBox にドラッグした列。移行後:

frmMain.pas
procedure TFormMain.FormCreate(Sender: TObject);
begin
  DoCreateData;
  // TDBGrid: DataSource := dsProducts. sgcGrid: a TsgcGridSource_DataSet
  // with DataSet, Grid and KeyField (set on the form). The grid columns are
  // designed on the form like TDBGrid.Columns, matched by FieldName
{$IFDEF SGC_GRID_EDIT}
  // TDBGrid dgEditing posts edits to the dataset: WriteBack. The current
  // record follows the focused row and back: SyncCursor
  SourceDB.WriteBack := True;
  SourceDB.SyncCursor := True;
{$ELSE}
  chkEditing.Checked := False;
  chkEditing.Enabled := False;
  chkEditing.Caption := 'Editing (Registered edition)';
  GridDB.ReadOnly := True;
{$ENDIF}
  SourceDB.Active := True;
  SourceCx.Active := True;
  chkOptionsClick(nil);
  // cxGrid: drag a column to the GroupByBox. sgcGrid: ShowGroupPanel, and
  // GroupBy from code (cxGridDBColumn.GroupIndex is Columns[i].GroupIndex)
  GridCx.GroupBy(['Category']);

cxGrid のテーブルビューの機能を設計時に設定

移行前: フッター集計項目、グループ化ボックス、フィルター行、検索パネル、バンド付きテーブルビュー。移行後: フォーム上の列プロパティとグリッドプロパティです。

frmMain.dfm
Columns = <
  item
    FieldName = 'ID'
    Caption = 'ID'
    DataType = gdtInteger
    Width = 60
    SummaryType = gsuCount
    Band = 0
  end
  item
    FieldName = 'Product'
    Caption = 'Product'
    Width = 220
    Band = 0
  end
  item
    FieldName = 'Category'
    Caption = 'Category'
    Width = 140
    Band = 0
  end
  item
    FieldName = 'Price'
    Caption = 'Price'
    DataType = gdtFloat
    Width = 110
    DisplayFormat = '#,##0.00'
    SummaryType = gsuAverage
    SummaryFormat = '#,##0.00'
    Band = 1
  end
  item
    FieldName = 'Qty'
    Caption = 'Qty'
    DataType = gdtInteger
    Width = 90
    SummaryType = gsuSum
    Band = 1
  end>
FormatRules = <>
ShowFooter = True
ShowGroupPanel = True
GroupSummaries = True
ShowFilterButtons = True
ShowFilterRow = True
ShowFindPanel = True
Bands = <
  item
    Caption = 'Product'
  end
  item
    Caption = 'Stock and price'
  end>

ベストフィット、レイアウト、カードビュー

移行前: ApplyBestFit、StoreToIniFile、RestoreFromIniFile、TcxGridDBCardView。移行後:

frmMain.pas
procedure TFormMain.btnBestFitClick(Sender: TObject);
begin
  // cxGridDBTableView.ApplyBestFit -> BestFitColumns (BestFitColumn(i) for
  // one column, BestFitOnDblClick for the header divider)
  GridCx.BestFitColumns;
end;

function TFormMain.DoLayoutFile: string;
begin
  Result := ExtractFilePath(ParamStr(0)) + 'migratefromdbgrid_layout.json';
end;

procedure TFormMain.btnSaveLayoutClick(Sender: TObject);
begin
  // cxGridDBTableView.StoreToIniFile -> SaveLayoutToFile (JSON: columns,
  // widths, order, sort, grouping, filters, summaries, bands, view)
  GridCx.SaveLayoutToFile(DoLayoutFile);
  DoShowStatus('layout saved to ' + ExtractFileName(DoLayoutFile));
end;

procedure TFormMain.btnLoadLayoutClick(Sender: TObject);
begin
  // cxGridDBTableView.RestoreFromIniFile -> LoadLayoutFromFile
  if GridCx.LoadLayoutFromFile(DoLayoutFile) then
    DoShowStatus('layout loaded')
  else
    DoShowStatus('no saved layout');
end;

procedure TFormMain.chkCardViewClick(Sender: TObject);
begin
{$IFDEF SGC_GRID_ADVANCED}
  // cxGridDBCardView (a second view of the level) -> ViewStyle gvsCard of
  // the same grid, CardOptions for the caption field and the card size
  if chkCardView.Checked then
    GridCx.ViewStyle := gvsCard
  else
    GridCx.ViewStyle := gvsTable;
{$ENDIF}
end;

フォームごとの 7 つの手順

データ層はそのままです。変更するのはグリッドとそのオプションだけです。

1

コンポーネントを配置する

sgcGrid のパッケージをインストールし、フォームに TsgcVCLGrid と TsgcGridSource_DataSet を配置します。

2

データセットを指定する

ソースの DataSet に、以前のグリッドが使っていたデータセットを設定します (グリッドに TDataSource はもう不要です)。続いて Grid と KeyField を設定します。KeyField には、インプレース更新と書き戻しに必要な一意のフィールドを指定します。

3

列を作り直す

FieldName、Caption、Width、DisplayFormat、Editable を持つ Columns を追加するか、Columns を空のままにして AutoColumns にフィールドから作成させます。

4

オプションを対応付ける

dgEditing には ReadOnly と WriteBack、dgRowSelect と dgMultiSelect には SelectionMode、dgIndicator には RowHeader、現在のレコードには SyncCursor を使います。

5

描画コードを移す

OnDrawColumnCell のコードを OnCustomDrawCell へ移します。色を設定するだけなら OnGetCellStyle へ移します。

6

cxGrid の機能を有効にする

ShowGroupPanel、ShowFooter、GroupSummaries、ShowFilterRow、ShowFilterButtons、ShowFindPanel。集計は Columns[i].SummaryType、バンドは Bands と Columns[i].Band、レイアウトは SaveLayoutToFile へ移します。

7

有効化する

データセットを開いたら、ソースの Active := True を設定します。

1 対 1 では対応しない点

移行を計画する前にお読みください。

MigrateFromDBGrid

カテゴリのルックアップフィールドを持つ 40 製品の TClientDataSet を、「From TDBGrid」と「From cxGrid」の 2 ページで表示します。

場所

sgcUI のインストーラーは Demos\Grid\Delphi\MigrateFromDBGrid に配置します。内容は MigrateFromDBGrid.dpr、frmMain.pas、frmMain.dfm、readme.html です。

実行方法

Delphi 7 から 13 のいずれかで MigrateFromDBGrid.dpr を開き、Win32 または Win64 でビルドして実行します。各ページには、TDBGrid または cxGrid の概念が、対応する sgcGrid の機能の隣に示されています。

セルフテスト

MigrateFromDBGrid.exe /test は、列とオプションのマッピング、コピーしたテキスト、グループ化、フッター集計、バンド、レイアウトの往復、Registered エディションでは書き戻しとカードビューを検証し、すべてのチェックに合格すると 0 で終了します。

両方のグリッドに公平を期すために述べると、cxGrid には sgcGrid にないものがあります。たとえば 1 つのグリッドコントロール内の複数のビューとレベル、INI レイアウトがあり、他にも多数のコントロールが付属します。アプリケーションがそれらに依存している場合、移行はこのページで示したものより大きくなります。上に挙げたグリッドの機能を使っていて、それを完全なソースコード付きで、Delphi 7 から 13 に対応する単一の製品として使いたいなら、sgcGrid が進むべき道です。機能の一覧は sgcGrid の製品ページをご覧ください。

最もお得な選択: All-AccesseSeGeCe の全製品にプレミアムサポートが付いて、年間 €1,059 からご利用いただけます。
All-Access の価格を見る

最初のグリッドを今日から移行

sgcGrid と MigrateFromDBGrid デモを含む sgcUI 体験版をダウンロードして、最初のフォームを移行しましょう。