I am trying to delete multiple selected rows from a DataGridView. When I try the code below, it will only delete just a few of the selected rows. An example, I have seven rows, I select 5 consecutive rows and press delete and only three get deleted.
IEnumerable<DataGridViewRow> dgvrs = from dgvrws in dgvChemicalInv.Rows.Cast<DataGridViewRow>()
where dgvrws.Selected.Equals(true)
select dgvrws;
foreach ( DataGridViewRow dgr in dgvrs )
{
dctchemri = dgr.Cells["DCT_CHEMRI"].Value.ToString();
index = dgvChemicalInv.CurrentRow.Index;
var chemObj = ( from chmObj in DCTProjectNodeObj.Chemicals
where chmObj.DCTChemRI.Equals(dctchemri)
select chmObj ).Single();
if ( sqlCmd.Delete_Chems(this.projID, (csDCTChemicalObj)chemObj) )
{
dgvChemicalInv.Rows.Remove(dgr);
}
if ( dgvChemicalInv.RowCount > 0 )
{
DCTProjectNodeObj.Chemicals.Remove((csDCTChemicalObj)chemObj);
}
else
{
DCTProjectNodeObj.Chemicals = new List<csDCTChemicalObj>();
DO_BtnSaveClickEvent();
}
}
Thank you,
Bill O.