Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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. Also see How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control Using the Designer.

private void MakeReadOnly()
{
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.AllowUserToDeleteRows = false;
    dataGridView1.ReadOnly = true;
}

This example requires:

Show:
© 2015 Microsoft