ikathegreat
ikathegreat

Reputation: 2321

allow next row to be add only once editing row is completed

I'm using a DataGridView and have AllowUserToAddRows set to True, so that once you begin editing a row, another blank row gets added to the control.

However I would to know a way to prevent the next row from getting added until the user completes entering the data for the row before it. This means that when all of the columns of the DataGridView (which are textboxes) become not empty, the next blank row will get added.

My alternative is to setup a row validation function that gets call when a user tries to leave an incomplete row.

Upvotes: 1

Views: 1039

Answers (1)

Chuck Wilbur
Chuck Wilbur

Reputation: 2620

I would think you would set AllowUserToAddRows to false, then add a handler for the cell changed event. In the handler, if the edit is in the last row, check whether all the edits in that row have something in them. If they do, add a row.

Edit: Something like this:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.RowCount > 0 && e.RowIndex == dataGridView1.RowCount - 1)
    {
        foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
        {
            if (cell.Value == null)
            {
                return;
            }
        }
        dataGridView1.Rows.Add();
    }
}

Upvotes: 4

Related Questions