protected void btnExcluir_Click(object sender, EventArgs e) { try { ContatoControl contatoDal = new ContatoControl(); ContatoFields contatoObj = new ContatoFields(); ImageButton imgButton; imgButton = (ImageButton)sender; //converter objeto para checkbox GridViewRow row = (GridViewRow)imgButton.Parent.Parent; // pegar a linha pai desta checkbox int idContato = Convert.ToInt32(gvPrincipal.DataKeys[row.RowIndex]["idContato"].ToString());//pegar o código da datakey da linha AssociacaoControl associacaoDal = new AssociacaoControl(); AssociacaoFields associacaoObj = associacaoDal.FindByfkContato(contatoObj.idContato); if (associacaoObj != null) associacaoDal.Delete(associacaoObj.idAssociacao); contatoDal.Delete(Convert.ToInt32(idContato)); MessageBox.Show("Contato apagado com sucesso.", MessageBox.MessageType.Success); DoSearch(); } catch (Exception ex) { MessageBox.Show(ex.Message, MessageBox.MessageType.Error); } }