Hi
Is there any best practices when databinding entities in WinForms applications?
I have created an user control for maintaining an entity. I wan't to reuse the enitity, because the entity might be shared with other user controls.
I came up with the solution shown below. The primary key is entered in one or more unbound controls (in the example selskabNrBE) and when the key is complete the entity is fetched and the properties shown in the bound controls.
In my user control constructor I create the entity and the binding source and sets up the data binding.
In the key complete event (in the example selskabNrBE_OnLeave) I use the Reset method descriped in the tread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5781&HighLight=1 in order to clear the entity. After fetching I call the ResetCurrentItem method on the binding source, in order to update the UI.
Is there any "one liners" for clearing all entity collections on the entity or do I have to implement it my self?
Why is it nessecary to call ResetCurrentItem method on the binding source, in order to update the UI after fetching the entity?
private SelskabEntity _selskabEntity;
private BindingSource _selskabBindingSource;
public SelskabUserControl()
{
InitializeComponent();
_selskabEntity = new SelskabEntity();
_selskabBindingSource = new BindingSource();
_selskabBindingSource.DataSource = _selskabEntity;
selskabNavnLabel.Text = "";
selskabNavnTextEdit.DataBindings.Add("Text", _selskabBindingSource, "SelskabNavn");
adresse1TextEdit.DataBindings.Add("Text", _selskabBindingSource, "Adresse1");
adresse2TextEdit.DataBindings.Add("Text", _selskabBindingSource, "Adresse2");
postNrTextEdit.DataBindings.Add("Text", _selskabBindingSource, "PostNr");
admStartDatoTextEdit.DataBindings.Add("Text", _selskabBindingSource, "AdmStartDato");
admStopDatoTextEdit.DataBindings.Add("Text", _selskabBindingSource, "AdmStopDato");
}
private void selskabNrBE_Leave(object sender, EventArgs e)
{
if (((ButtonEdit)sender).IsModified)
{
int selskabNr = Convert.ToInt32(((ButtonEdit)sender).Text);
_selskabEntity.Reset();
_selskabEntity.SelskabNr = selskabNr;
using (DataAccessAdapter adapter = new DataAccessAdapter())
adapter.FetchEntity(_selskabEntity);
selskabNavnLabel.Text = _selskabEntity.SelskabNavn;
_selskabBindingSource.ResetCurrentItem();
}
}
Regards,
Nikolaj Halvorsen