Problem when using CancelEdit () from BindingSource C #

0

I'm creating a CRUD and I'm using the BindingSource component with the DataSource Tipped with my Parent class.

In the form I put a textbox attached to my bindingSource in the Name property of my Country object. Also added a 2 buttons, the new one, where I insert a new Country to register in the BindingSource, and cancel canceling the edition.

My question is, how do I stop bindingSource from editing? for example, I click on the new button, I write something in the textBox that is connected to the BindingSource and when clicked on cancel it return to the previous state, ie, to defy what I ended up writing in my TextBox.

Because when I click the new button I am creating a new instance of my Parent object, that is, all null fields, and when I type in the textBox (Name) a text and click cancel, the bindingSource put that text written in my field Name, so when I click cancel and BindingSource's CancelEdit () executes the previous state of my object should not return to what it was before?

publicpartialclassPais:EntityBase{publicPais():base(){}publicoverridestringTableName{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();
    }
}
    
asked by anonymous 09.06.2017 / 20:02

1 answer

0

What you can understand is that when you click the new button, C # creates a new instance. Try to destroy the instance manually. Here's how:

private void btnCancelar_Click(object sender, EventArgs e)
{
    ~NomeDaClasse
    bindingSource1.CancelEdit();
}
    
12.06.2017 / 19:17