private void FrmCollectionList_VisibleChanged(object sender, EventArgs e) { // if change for invisible to visible if (!isVisible && this.Visible) { signcollectionsList = db.GetCollectionList(); List <signcollection> temp = signcollectionsList; foreach (DataGridViewRow row in dgv_collection.Rows) { signcollection sc = temp.FirstOrDefault(i => i.collectionId == row.Cells[0].Value.ToString()); if (sc == null) { dgv_collection.Rows.Remove(row); continue; } organisation o = db.GetOrganisation(sc.organisationId); int signNum = db.GetNoOfSign(sc.collectionId); int datasetNum = db.GetNoOfDataset(sc.collectionId); row.Cells[1].Value = sc.name; row.Cells[2].Value = o.name; row.Cells[3].Value = signNum; row.Cells[4].Value = datasetNum; temp.Remove(sc); } if (temp.Count > 0) { foreach (signcollection sc in temp) { organisation o = db.GetOrganisation(sc.organisationId); int signNum = db.GetNoOfSign(sc.collectionId); int datasetNum = db.GetNoOfDataset(sc.collectionId); dgv_collection.Rows.Add(sc.collectionId, sc.name, o.name, signNum, datasetNum); } } } isVisible = this.Visible; }