DataGridView.ReadOnly 属性

定义

获取或设置一个值,该值指示用户是否可以编辑 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. 默认值为 falseThe 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 属性设置为 trueThe 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. 通常情况下,可将该异常对象强制转换为类型 FormatExceptionThe exception object can typically be cast to type FormatException.

示例

下面的代码示例演示如何在主要用于显示的 @no__t 0 控件中使用此属性。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

注解

如果单元格、单元格的行、单元格的列或 @no__t @no__t @no__t 的值为-1,则单元格是只读的。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.

适用于

另请参阅