0
I am instantiating a form from a double click on a Datagridview from another form, where through this return the information from that line to the form.
It is working properly but when I close this form and double click again on the same line or another to open the form again I get the following error:
Unable to access a discarded object.
The code where the error occurs is this:
private void dgvDados_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvDados.SelectedRows.Count > 0)
{
this.exibirdados.txtcnpj.Text = dgvDados.SelectedRows[0].Cells[0].Value.ToString();
this.exibirdados.dtpcadastro.Text = dgvDados.CurrentRow.Cells[1].Value.ToString();
this.exibirdados.txtrazao.Text = dgvDados.CurrentRow.Cells[2].Value.ToString();
this.exibirdados.cmboperadora.Text = dgvDados.CurrentRow.Cells[3].Value.ToString();
this.exibirdados.txtlinhas.Text = dgvDados.CurrentRow.Cells[4].Value.ToString();
this.exibirdados.cmbClassificacao.Text = dgvDados.CurrentRow.Cells[5].Value.ToString();
this.exibirdados.dtpvigencia.Text = dgvDados.CurrentRow.Cells[6].Value.ToString();
this.exibirdados.txtcontrato.Text = dgvDados.CurrentRow.Cells[7].Value.ToString();
this.exibirdados.cmbFidelidade.Text = dgvDados.CurrentRow.Cells[8].Value.ToString();
this.exibirdados.txtvalorgasto.Text = dgvDados.CurrentRow.Cells[9].Value.ToString();
this.exibirdados.txtfixoempresa.Text = dgvDados.CurrentRow.Cells[10].Value.ToString();
this.exibirdados.txtgestor.Text = dgvDados.CurrentRow.Cells[11].Value.ToString();
this.exibirdados.txtcelular.Text = dgvDados.CurrentRow.Cells[12].Value.ToString();
this.exibirdados.txtfixogestor.Text = dgvDados.CurrentRow.Cells[13].Value.ToString();
this.exibirdados.txtemail.Text = dgvDados.CurrentRow.Cells[14].Value.ToString();
this.exibirdados.txtobs.Text = dgvDados.CurrentRow.Cells[15].Value.ToString();
}
this.exibirdados.ShowDialog(); //aqui acontece o erro
}
Form instance code:
public partial class TelaInicio : MetroFramework.Forms.MetroForm
{
internal ExibirDados exibirdados = null;
public TelaInicio(ExibirDados exibirdados)
{
InitializeComponent();
this.exibirdados = exibirdados;
}
Use the
new
on the lineinternal ExibirDados exibirdados = new ExibirDados();
– Wilson Faustino
@W.Faustino proceeds with the same error
– Patrick Perdigão
Tries to create the object
ExibirDados
withindgvDados_CellContentDoubleClick
without using thethis.
. I know you’re not gonna keep the GC clean but... it might work well.– Wilson Faustino
@W.Faustino if I do this it will open and close without error, but it will not recover the information for the form, since I am receiving them from the instance
exibirdados
– Patrick Perdigão
Paste all the code there! It’s easier for us to help you.
– Wilson Faustino
I managed to sort out, in part what you said was right, thank you
– Patrick Perdigão