Problem using Bindingsource C#Canceledit()

Asked

Viewed 70 times

0

I am creating a CRUD and I am using the Bindingsource component with Datasource Typed with my Parents class.

In the form I placed a textbox linked to my bindingSource in the property Name of my object Parents. Also put a 2 buttons, the new, where insert a new Country to register in Bindingsource, and cancel that cancels the edition.

My question is, how do I get bindingSource to cancel an edition I make? for example, I click the New Button, write something in the textbox that is linked to Bindingsource and when clicked cancel it go back to the previous state, that is, undo what I ended up writing in my textBox.

Because by clicking the new button I am creating a new Instance of my Parents object, ie all null fields, and when I type in the textBox(Name) a text and click cancel, bindingSource put this text written in my Name field, then when I click cancel and Bindingsource Canceledit() is executed the previous state of my object should not return to what it was before?

inserir a descrição da imagem aqui

public partial class Pais : EntityBase
{
    public Pais() : base() { }
    public override string TableName { get { return "PAIS"; } }
    public override int Handle { get; set; }
    public string Nome { get; set; }
    public string Sigla { get; set; }
    public virtual ICollection<Estado> Estados { get; set; }
    public override DateTime DataCadastro { get; set; }
    public override string UsuarioCadastro { get; set; }
    public override DateTime? DataAlteracao { get; set; }
    public override string UsuarioAlteracao { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.bindingSource1.DataSource = typeof(App.Erp.Data.Entities.Pais);
    }

    private void btnNovo_Click(object sender, EventArgs e)
    {
        bindingSource1.Add(new Pais());
    }

    private void btnCancelar_Click(object sender, EventArgs e)
    {
        bindingSource1.CancelEdit();
    }
}

1 answer

0

What you can understand is that when you click the new button, c# creates a new instance. It tries to destroy the instance manually. It does so:

private void btnCancelar_Click(object sender, EventArgs e)
{
    ~NomeDaClasse
    bindingSource1.CancelEdit();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.