次の方法で共有


方法 : Windows フォーム DataGridView コントロールの既定のセル スタイルを設定する

DataGridView コントロールを使用すると、既定のセル スタイルをコントロール全体、および特定の列と行に指定できます。 これらの既定のセル スタイルは、コントロール レベルから、下位の列レベル、行レベル、セル レベルへと、順にフィルター処理されていきます。 セル レベルで特定の DataGridViewCellStyle プロパティが設定されていない場合は、行レベルの既定のプロパティ設定が使用されます。 行レベルでもプロパティが設定されていない場合は、列レベルの既定の設定が使用されます。 さらに、列レベルでもプロパティが設定されていない場合は、既定の DataGridView 設定が使用されます。 この設定では、複数のレベルでプロパティの設定値を複製する必要はありません。 各レベルで、上位のレベルと異なるスタイルを指定するだけです。 詳細については、「Windows フォーム DataGridView コントロールでのセルのスタイル」を参照してください。

Visual Studio では、このタスクに対する広範なサポートが用意されています。 詳細については 方法 : デザイナを使用して Windows フォーム DataGridView コントロールの既定のセル スタイルとデータ形式を設定する および 方法 : デザイナを使用して Windows フォーム DataGridView コントロールの既定のセル スタイルとデータ形式を設定する および 方法 : デザイナを使用して Windows フォーム DataGridView コントロールの既定のセル スタイルとデータ形式を設定する および 方法 : デザイナーを使用して Windows フォーム DataGridView コントロールの既定のセル スタイルとデータ形式を設定する.

既定のセル スタイルをプログラムで設定するには

  1. DataGridView.DefaultCellStyle プロパティを使用して取得した DataGridViewCellStyle の各プロパティを設定します。

    Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige
    Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 12)
    
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
    
  2. 複数の行および列で使用する新しい DataGridViewCellStyle オブジェクトを作成して初期化します。

    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red
    
    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green
    
    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;
    
    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;
    
  3. 特定の行および列の DefaultCellStyle プロパティを設定します。

    With Me.dataGridView1
        .Rows(3).DefaultCellStyle = highlightCellStyle
        .Rows(8).DefaultCellStyle = highlightCellStyle
        .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
        .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
    End With
    
    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
    

使用例

Private Sub SetDefaultCellStyles()

    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red

    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green

    With Me.dataGridView1
        .DefaultCellStyle.BackColor = Color.Beige
        .DefaultCellStyle.Font = New Font("Tahoma", 12)
        .Rows(3).DefaultCellStyle = highlightCellStyle
        .Rows(8).DefaultCellStyle = highlightCellStyle
        .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
        .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
    End With

End Sub
private void SetDefaultCellStyles()
{
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);

    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;

    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;

    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
}

コードのコンパイル

この例で必要な要素は次のとおりです。

信頼性の高いプログラミング

特に大きなデータ セットを処理する場合に最大のスケーラビリティを実現するには、各要素に個別のスタイル プロパティを設定する代わりに、同じスタイルを使用する複数の行、列、またはセルで DataGridViewCellStyle オブジェクトを共有します。 また、共有行を作成し、DataGridViewRowCollection.SharedRow プロパティを使用してその共有行にアクセスする必要もあります。 詳細については、「Windows フォーム DataGridView コントロールを拡張するための推奨される手順」を参照してください。

参照

処理手順

方法 : Windows フォーム DataGridView コントロールに交互の行のスタイルを設定する

参照

DataGridView

DataGridViewCellStyle

DataGridView.DefaultCellStyle

DataGridViewBand.DefaultCellStyle

概念

Windows フォーム DataGridView コントロールでのセルのスタイル

Windows フォーム DataGridView コントロールを拡張するための推奨される手順

その他の技術情報

Windows フォームの DataGridView コントロールの基本的な書式設定およびスタイル設定