DataGridView.ReadOnly DataGridView.ReadOnly DataGridView.ReadOnly DataGridView.ReadOnly Property

定義

ユーザーが DataGridView コントロールのセルを編集できるかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the user can edit the cells of the DataGridView control.

public:
 property bool ReadOnly { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(true)]
public bool ReadOnly { get; set; }
member this.ReadOnly : bool with get, set
Public Property ReadOnly As Boolean

プロパティ値

ユーザーがtrue コントロールのセルを編集できない場合は DataGridView 。それ以外の場合は falsetrue if the user cannot edit the cells of the DataGridView control; otherwise, false. 既定値は、false です。The default is false.

例外

このプロパティを設定したときに指定した値は true です。現在のセルは編集モードですが、現在のセルにコミットできない変更が含まれています。The specified value when setting this property is true, the current cell is in edit mode, and the current cell contains changes that cannot be committed.

このプロパティの設定時に指定した値によってコントロールが編集モードに入りますが、編集セルの値の初期化は失敗し、DataError イベントのハンドラーがないか、ハンドラーが ThrowException プロパティを true に設定しているかのいずれかの状態になります。The specified value when setting this property would cause the control to enter edit mode, but initialization of the editing cell value failed and either there is no handler for the DataError event or the handler has set the ThrowException property to true. 通常、例外オブジェクトは型 FormatException にキャストできます。The exception object can typically be cast to type FormatException.

主に表示用のDataGridViewコントロールでこのプロパティを使用する方法を次のコード例に示します。The following code example illustrates the use of this property in a DataGridView control intended primarily for display. この例では、コントロールの外観がいくつかの方法でカスタマイズされており、コントロールは対話機能を制限するように構成されています。In this example, the visual appearance of the control is customized in several ways and the control is configured for limited interactivity. この例は、 DataGridViewCellStyleクラスの概要で使用できるより大きな例の一部です。This example is part of a larger example available in the DataGridViewCellStyle class overview.

// 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);
}
' Configures the appearance and behavior of a DataGridView control.
Private Sub 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. 
    Dim font As New Font( _
        dataGridView1.DefaultCellStyle.Font.FontFamily, 25, FontStyle.Bold)
    Try
        dataGridView1.Columns("Rating").DefaultCellStyle.Font = font
    Finally
        font.Dispose()
    End Try

End Sub

注釈

ReadOnlyプロパティの値がtrueセル、セルの行、セルDataGridViewの列、またはである場合、セルは読み取り専用です。A cell is read-only if the value of the ReadOnly property is true for the cell, the cell's row, the cell's column, or the DataGridView.

適用対象

こちらもご覧ください