title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
---|---|---|---|---|---|---|---|---|---|
Make Columns Read-Only in DataGridView Control |
Learn how to make columns read-only in the Windows Form DataGridView Control to prevent users from editing cells in that column. |
03/30/2017 |
|
|
2bb73ebb-1a55-4362-9fda-e50574c087d5 |
Not all data is meant for editing. In the xref:System.Windows.Forms.DataGridView control, the column xref:System.Windows.Forms.DataGridViewColumn.ReadOnly%2A property value determines whether users can edit cells in that column. For information about how to make the control entirely read-only, see How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control.
There is support for this task in Visual Studio. Also see How to: Make Columns Read-Only in the Windows Forms DataGridView Control Using the Designer.
-
Set the xref:System.Windows.Forms.DataGridViewColumn.ReadOnly%2A?displayProperty=nameWithType property to
true
.[!code-csharpSystem.Windows.Forms.DataGridViewMisc#064] [!code-vbSystem.Windows.Forms.DataGridViewMisc#064]
This example requires:
-
A xref:System.Windows.Forms.DataGridView control named
dataGridView1
with a column namedCompanyName
. -
References to the xref:System?displayProperty=nameWithType and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.
- xref:System.Windows.Forms.DataGridView
- xref:System.Windows.Forms.DataGridView.Columns%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridViewColumn.ReadOnly%2A?displayProperty=nameWithType
- Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control
- How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control