True DBGrid for WinForms
ベストプラクティス

In order to leverage the full potential of True DBGrid and turn it to your advantage, there are a number of best practices to keep in mind.

The following topics will guide you through some of the best practices that can be followed while working with True DBGrid.

ヒント 1: DataSource リセット時にグリッドのレイアウトを維持する

コードで DataSource をリセットすると、グリッドにすべてのデータが表示され、デザイナで作成した初期のレイアウトは維持されません。

HoldFields パラメータを True に設定して SetDataBinding を使用すると、グリッドのレイアウトデザインを維持できます。例を示します。

C#
コードのコピー
this.c1TrueDBGrid1.SetDataBinding(this.DbDataSet, "Customer", true);

ヒント 2: FetchCellStyle イベントを使用して列のスタイルを設定する

列は移動したりソートされる可能性があるため、通常、表示列インデックスと列インデックスを使用する場合は、これらが異なる列を参照することに注意する必要があります。

特定の表示列にスタイルを関連付けることができます。次の例は、FetchCellStyle イベントを使用して、1 つの表示列にスタイルを関連付けています。

C#
コードのコピー
private void c1TrueDBGrid1_FetchCellStyle(object sender, FetchCellStyleEventArgs e)
{
    if (this.c1TrueDBGrid1.Splits[0].DisplayColumns[e.Col].DataColumn.Value.GetType() == typeof(string))
    {
        e.CellStyle.ForeColor = Color.Red;
    }
}

ヒント 3: 現在の列と行の取得

It can be really useful to find out what cell a user is interacting with, or what cell is currently selected. Getting the column number and row number of the selected cell is very simple to do.

For example, the following code determines and displays the row and column number of the current cell:

C#
コードのコピー
this.c1TrueDBGrid1.Row = this.c1TrueDBGrid1.RowContaining(c1TrueDBGrid1.Row);
this.c1TrueDBGrid1.Col = this.c1TrueDBGrid1.ColContaining(c1TrueDBGrid1.Col);
MessageBox.Show("The number of the column is " + this.c1TrueDBGrid1.Col + " the row row number is " + this.c1TrueDBGrid1.Row);

ヒント 4: 階層データビューで、グリッドの折りたたみを禁止する

グリッドが階層データビューで表示されている場合は、ユーザーがグリッドを折りたたんで通常のデータビューにすることができないように設定できます。

Collapse イベントを使用して e.Cancel を True に設定すると、ユーザーは展開アイコンを折りたためなくなります。例を示します。

C#
コードのコピー
private void c1TrueDBGrid1_Collapse(object sender, BandEventArgs e)
{
    e.Cancel = true;
}