How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control
Sometimes you will want to prevent users from entering new rows of data or deleting existing rows in your DataGridView control. The AllowUserToAddRows property indicates whether the row for new records is present at the bottom of the control, while the AllowUserToDeleteRows property indicates whether rows can be removed. The following code example uses these properties and also sets the ReadOnly property to make the control entirely read-only.
There is support for this task in Visual Studio. How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control Using the Designer
How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control Using the Designer
How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control Using the Designer
How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control Using the Designer
Example
Private Sub MakeReadOnly()
With dataGridView1
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.ReadOnly = True
End With
End Sub
private void MakeReadOnly()
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.ReadOnly = true;
}
Compiling the Code
This example requires:
A DataGridView control named
dataGridView1
.References to the System and System.Windows.Forms assemblies.
See Also
Reference
DataGridView
System.Windows.Forms.DataGridView.AllowUserToAddRows
System.Windows.Forms.DataGridView.ReadOnly
System.Windows.Forms.DataGridView.AllowUserToAddRows
System.Windows.Forms.DataGridView.AllowUserToDeleteRows
Other Resources
Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control