DataGridView.ReadOnly 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出使用者是否可以編輯 DataGridView 控制項的儲存格。
public:
property bool ReadOnly { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(true)]
public bool ReadOnly { get; set; }
[<System.ComponentModel.Browsable(true)>]
member this.ReadOnly : bool with get, set
Public Property ReadOnly As Boolean
屬性值
如果使用者不能編輯true
控制項的儲存格,則為 DataGridView ;否則為 false
。 預設為 false
。
- 屬性
例外狀況
當設定這個屬性為 true
、目前的儲存格處於編輯模式,以及目前的儲存格包含無法認可的變更時所指定的值。
設定這個屬性時所指定的值會讓控制項進入編輯模式,但編輯儲存格值的初始設定失敗,而且 DataError 事件沒有處理常式或處理常式已將 ThrowException 屬性設定為 true
。 例外狀況物件通常可轉換成 FormatException 類型。
範例
下列程式碼範例說明在主要用於顯示的控制項中使用 DataGridView 這個屬性。 在此範例中,控制項的視覺外觀會以數種方式自訂,而且控制項會設定為有限的互動性。 此範例是類別概觀中較大範例的 DataGridViewCellStyle 一部分。
// 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
備註
如果 屬性 true
的值 ReadOnly 是針對儲存格、儲存格的資料列、儲存格的資料行或 DataGridView ,則儲存格是唯讀的。