方法: Windows フォーム DataGridView コントロールの既定のセル スタイルを設定する
DataGridView コントロールを使用して、コントロール全体、および特定の列と行の既定のセル スタイルを指定できます。 これらは既定でフィルターを下に移動し、コントロール レベルから列レベルへ、次に行レベルへ、その次にセル レベルへ移動します。 特定の DataGridViewCellStyle プロパティがセル レベルで設定されていないと、行レベルで既定のプロパティの設定が使用されます。 行レベルでもプロパティが設定されていない場合、既定の列の設定が使用されます。 最後に、列レベルでもプロパティが設定されていない場合、既定の DataGridView の設定が使用されます。 この設定により、複数のレベルでプロパティの設定を複製する必要がなくなります。 各レベルでは、上位のレベルとは異なるスタイルだけを指定します。 詳細については、「Windows フォーム DataGridView コントロールでのセルのスタイル」を参照してください。
Visual Studio では、このタスクに対する広範なサポートが用意されています。 また、「方法: デザイナーを使用して Windows フォーム DataGridView コントロールの既定のセル スタイルとデータ形式を設定する」も参照してください。
既定値のセル スタイルをプログラムで設定するには
DataGridView.DefaultCellStyle プロパティによって取得された DataGridViewCellStyle のプロパティを設定します。
this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige; this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 12)
複数の行と列によって使用される新しい DataGridViewCellStyle オブジェクトを作成して初期化します。
DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle(); highlightCellStyle.BackColor = Color.Red; DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle(); currencyCellStyle.Format = "C"; currencyCellStyle.ForeColor = Color.Green;
Dim highlightCellStyle As New DataGridViewCellStyle highlightCellStyle.BackColor = Color.Red Dim currencyCellStyle As New DataGridViewCellStyle currencyCellStyle.Format = "C" currencyCellStyle.ForeColor = Color.Green
特定の行と列の
DefaultCellStyle
プロパティを設定します。this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle; this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle; this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle = currencyCellStyle; this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle = currencyCellStyle;
With Me.dataGridView1 .Rows(3).DefaultCellStyle = highlightCellStyle .Rows(8).DefaultCellStyle = highlightCellStyle .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle End With
例
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;
}
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
コードのコンパイル
この例で必要な要素は次のとおりです。
dataGridView1
という名前の DataGridView コントロール。System、System.Drawing、および System.Windows.Forms の各アセンブリへの参照。
信頼性の高いプログラミング
非常に大きなデータ セットを処理するときに最大限のスケーラビリティを実現するには、各要素のスタイルのプロパティを個別に設定するのではなく、同じスタイルを使用する複数の行、列、またはセルで DataGridViewCellStyle オブジェクトを共有してください。 さらに、DataGridViewRowCollection.SharedRow プロパティを使用して、共有された行を作成してアクセスする必要があります。 詳細については、「 Windows フォーム DataGridView コントロールを拡張するための推奨される手順」を参照してください。
関連項目
.NET Desktop feedback