英語で読む

次の方法で共有


DataGridView.DefaultCellStyle プロパティ

定義

その他のセル スタイル プロパティが設定されていない場合、DataGridView のセルに適用される既定のセル スタイルを取得または設定します。

C#
public System.Windows.Forms.DataGridViewCellStyle DefaultCellStyle { get; set; }

プロパティ値

既定スタイルとして適用される DataGridViewCellStyle

次のコード例は、主に表示を目的としたコントロールでこのプロパティを DataGridView 使用する方法を示しています。 この例では、コントロールの視覚的な外観がいくつかの方法でカスタマイズされ、コントロールは限られた対話機能用に構成されています。 この例は、クラスの概要で使用できるより大きな例の DataGridViewCellStyle 一部です。

C#
// Configures the appearance and behavior of a DataGridView control.
private void InitializeDataGridView()
{
    // Initialize basic DataGridView properties.
    dataGridView1.Dock = DockStyle.Fill;
    dataGridView1.BackgroundColor = Color.LightGray;
    dataGridView1.BorderStyle = BorderStyle.Fixed3D;

    // Set property values appropriate for read-only display and 
    // limited interactivity. 
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.AllowUserToDeleteRows = false;
    dataGridView1.AllowUserToOrderColumns = true;
    dataGridView1.ReadOnly = true;
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    dataGridView1.MultiSelect = false;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
    dataGridView1.AllowUserToResizeColumns = false;
    dataGridView1.ColumnHeadersHeightSizeMode = 
        DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
    dataGridView1.AllowUserToResizeRows = false;
    dataGridView1.RowHeadersWidthSizeMode = 
        DataGridViewRowHeadersWidthSizeMode.DisableResizing;

    // Set the selection background color for all the cells.
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

    // Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
    // value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
    dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;

    // Set the background color for all rows and for alternating rows. 
    // The value for alternating rows overrides the value for all rows. 
    dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray;

    // Set the row and column header styles.
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black;
    dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black;

    // Set the Format property on the "Last Prepared" column to cause
    // the DateTime to be formatted as "Month, Year".
    dataGridView1.Columns["Last Prepared"].DefaultCellStyle.Format = "y";

    // Specify a larger font for the "Ratings" column. 
    using (Font font = new Font(
        dataGridView1.DefaultCellStyle.Font.FontFamily, 25, FontStyle.Bold))
    {
        dataGridView1.Columns["Rating"].DefaultCellStyle.Font = font;
    }

    // Attach a handler to the CellFormatting event.
    dataGridView1.CellFormatting += new
        DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}

注釈

コントロールは DataGridView 、 型の他のプロパティからスタイルを継承する cell InheritedStyle プロパティで示されるスタイルを使用してセルを表示します DataGridViewCellStyle。 プロパティで指定されたスタイルは、次の DefaultCellStyle プロパティで指定されたスタイルによってオーバーライドされた場合を除き、すべてのセルに影響します。

詳細については、「Windows フォーム DataGridView コントロールでのセルのスタイル」を参照してください。

このプロパティを取得すると、 DataGridViewCellStyle プロパティにまだアクセスされていない場合は、既定値を持つ が作成されます。 多くの行に対してこのプロパティを取得すると、パフォーマンスに影響を与える可能性があります。 可能な限り、 を DataGridViewCellStyle 使用して複数の行に対してこのプロパティを設定します。 詳細については、「 Windows フォーム DataGridView コントロールを拡張するための推奨される手順」を参照してください。

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください