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 이고, 그렇지 않으면 false입니다.true 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.

적용 대상

추가 정보