I have an editable datagridview which has "Amount" as one of the column. Whenever user tries to enter a non numeric value in the amount column's cell and tab off, I display a message box stating that "The value is invalid. Press OK to correct or Cancel to delete the row". The moment user clicks on Cancel button to delete the row, I get exception stating that I can't performed this operation in the given event.
After doing some R&D on it I came to know that I can't remove a row from the grid in cell's validating, enter , click .events. The only event where I can remove the row is Cell's EndEdit event. Can anyone suggest me how can I remove the row from a datagridview form control in a interactive manner as described above
Here is the code which I have written:
private void grdFund_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string formatValue = (string)e.FormattedValue;
if (!IsNumeric(formatValue) || Convert.ToDecimal(e.FormattedValue) <= 0M)
{
if (MessageBox.Show(MessageBoxOption.OKCancelQuestion, 99999, "The funds value entered is invalid. Enter OK to Correct or Cancel to Delete.") == "Cancel")
{
grdFundGrid.Rows.Remove(grdFundGrid.Rows[e.RowIndex])
}
else
{
e.Cancel = true;
return;
}
}
......(some other stuff..)
}

How can I remove a row from the datagridview control in an interactive manner
Milos Kompjuteras
You could do something like this:
private bool deleteRow;
private void grdFundGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string formatValue = (string)e.FormattedValue;
if (!IsNumeric(formatValue) || Convert.ToDecimal(e.FormattedValue) <= 0M)
{
if (MessageBox.Show(MessageBoxOption.OKCancelQuestion, 99999,
"The funds value entered is invalid. Enter OK to Correct or Cancel to Delete.") == "Cancel")
{
deleteRow = true;
}
else
{
e.Cancel = true;
return;
}
}
}
void grdFundGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (deleteRow)
{
grdFundGrid.Rows.Remove(grdFundGrid.Rows[e.RowIndex]);
deleteRow = false;
}
}