private void AtualizarGrid(Endereco Endereco, bool booExcluir) { try { object FiltroExcluir = null; if (booExcluir) { FiltroExcluir = (LocalizarFiltroExcluir(dgwColecao) as Endereco).GetChavePrimaria(); dgwColecao.DataSource = null; enderecoCollection.Remove(Endereco); } else { if (Endereco != null) { var Item = from p in enderecoCollection where p.GetChavePrimaria() == Endereco.GetChavePrimaria() select p; if (Item.Count() == 1) { Endereco EnderecoSelecionado = Item.First(); EnderecoSelecionado = Endereco; } else { enderecoCollection.Add(Endereco); } } else { while (backgroundWorker1.IsBusy) { Application.DoEvents(); } object[] args = new object[2]; args[0] = "VMPesquisarEnderecoCliente"; args[1] = this.cliente.Pessoa.IDPessoa; backgroundWorker1.RunWorkerAsync(args); while (backgroundWorker1.IsBusy) { Application.DoEvents(); } } } dgwColecao.DataSource = null; if (enderecoCollection.Count() > 0) { dgwColecao.DataSource = enderecoCollection.ToList <Endereco>(); } txtRegistro.Text = dgwColecao.Rows.Count.ToString(); //if (objEndereco != null) // LocalizarIndexLinha(dgwColecao, FiltroExcluir == null ? objEndereco.GetChavePrimaria() : FiltroExcluir); dgwColecao.Update(); dgwColecao.Refresh(); if (dgwColecao.RowCount > 0) { dgwColecao.Focus(); } } catch (Exception ex) { Exceptionerro(ex); } }