I am trying to remove the selected rows from a DataGridView
but I am getting the following error message:
Specified cast is not valid.
On this line of code:
this.DGV_INVOICE.Rows.Cast<DataGridViewRow>().Where(ROW => (bool?)ROW.Cells[0].Value == true).ToList().ForEach(x => { this.DGV_INVOICE.Rows.Remove(x); });
Second way of trying to remove all the selected rows:
foreach (DataGridViewRow RowToDelete in DGV_INVOICE.Rows)
{
object Cell = RowToDelete.Cells["Edit_Checkbox"].Value;
if (Cell.ToString() == "True")
{
// Allow user to delete record from invoice
DGV_INVOICE.Rows.Remove(RowToDelete);
}
}
Loop backwards through your DataGridView's .Rows
collection so you don't lose the index while removing
for (int i = DGV_INVOICE.Rows.Count - 1; i >= 0; i--)
{
object Cell = DGV_INVOICE.Rows[i].Cells["Edit_Checkbox"].Value;
if (Cell.ToString() == "True")
DGV_INVOICE.Rows.RemoveAt(i);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments