Hi!
i tried to iterate the DataGrid Control with below code:
The foreach() loops only about 4 times when i have 10 rows in the DataGrid. And then it gives the exception "Object reference not set to an instance", and quits the loop. Why does the loop stop prematurely?Code:String strSelDvdCat = cbDvdCat.SelectedItem.ToString(); String strCat; int irowindex = 0; foreach (DataGridViewRow row in dgvViewDvds.Rows) { try { DataGridViewCell cell = row.Cells[2];//Cat colum (2) strCat = cell.Value.ToString(); //if (!(strSelDvdCat.ToLower().Contains(strCat.ToLower()))) { dgvViewDvds.Rows.RemoveAt(irowindex); MessageBox.Show(irowindex.ToString()); } irowindex++; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
Thanks for any help.