Basically it's a simple play with some events and enabling/disabling the AllowUserToAddRow property:
public Form1()
{
InitializeComponent();
//creating a test DataTable and adding an empty row
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Rows.Add(dt.NewRow());
//binding to the gridview
dataGridView1.DataSource = dt;
//Set the property AllowUserToAddRows to false will prevent a new empty row
dataGridView1.AllowUserToAddRows = false;
}
Now the events...
When the cell recognize the editing it will fire a event called CellBeginEdit. When it's in editing mode set the AllowUserToAddRows to false
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
}
When the cell recognize the end of editing it will fire a event called CellEndEdit. When it ends the editing mode check for your conditions. Based on the result set the AllowUserToAddRows to true ot keep it false.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//instead of MessageBox there could be as well your check conditions
if (MessageBox.Show("Cell edit finished, add a new row?", "Add new row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
dataGridView1.AllowUserToAddRows = true;
else dataGridView1.AllowUserToAddRows = false;
}