C datagridview prevent new row at end
You can prevent the user from adding a new row at the end of a DataGridView
by setting the AllowUserToAddRows
property to false
. Here's an example:
dataGridView1.AllowUserToAddRows = false;
This will prevent the user from adding a new row by clicking the "New Row" button at the bottom of the grid.
Alternatively, you can also use the ReadOnly
property to prevent the user from adding new rows. Set it to true
to make the grid read-only:
dataGridView1.ReadOnly = true;
However, this will also prevent the user from editing existing rows, so use this option with caution.
If you want to allow the user to add new rows, but only at specific times or under specific conditions, you can use the DataGridView
's UserAddedRow
event. This event is raised when the user adds a new row, and you can cancel the addition by setting the e.Cancel
property to true
.
For example:
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
if (/* your condition to prevent adding a new row */)
{
e.Cancel = true;
}
}
Note that the UserAddedRow
event is only raised when the user adds a new row, not when the grid is populated programmatically.