DataGridView コントロールを使用すると、コントロール全体および特定の列と行の既定のセル スタイルを指定できます。 既定では、コントロール レベルから列レベル、行レベル、セル レベルまでフィルター処理されます。 特定の DataGridViewCellStyle プロパティがセル レベルで設定されていない場合は、行レベルの既定のプロパティ設定が使用されます。 プロパティも行レベルで設定されていない場合は、既定の列設定が使用されます。 最後に、プロパティも列レベルで設定されていない場合は、既定の DataGridView 設定が使用されます。 この設定を使用すると、複数のレベルでプロパティ設定を複製する必要がなくなります。 各レベルで、上のレベルとは異なるスタイルを指定するだけです。 詳細については、「Windows フォーム DataGridView コントロールの セル のスタイル」を参照してください。
このタスクは Visual Studio で広範にサポートされています。 デザイナーを使用して Windows フォーム DataGridView コントロールの既定のセル スタイルとデータ形式を設定する方法
既定のセル スタイルをプログラムで設定するには
DataGridViewCellStyle プロパティを使用して取得した DataGridView.DefaultCellStyle のプロパティを設定します。
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
コードのコンパイル
この例では、次のものが必要です。
DataGridViewという名前の
dataGridView1
コントロール。System、System.Drawing、および System.Windows.Forms アセンブリへの参照。
堅牢なプログラミング
非常に大きなデータ セットを操作するときに最大限のスケーラビリティを実現するには、個々の要素のスタイル プロパティを個別に設定するのではなく、同じスタイルを使用する複数の行、列、またはセル間で DataGridViewCellStyle オブジェクトを共有する必要があります。 さらに、共有行を作成し、DataGridViewRowCollection.SharedRow プロパティを使用してアクセスする必要があります。 詳細については、「Windows フォーム DataGridView コントロールのスケーリングのベスト プラクティス」を参照してください。
こちらも参照ください
.NET Desktop feedback