Peter Dzuya

Peter Dzuya

  • NA
  • 313
  • 41.1k

Refreshing main form

Oct 14 2016 7:37 AM
Hi All,
I have two forms, one holding a list of all contacts named "frmContactsListing". The contact is selected from grid.
The other is named "frmEditContact", manages the edit of a single selected contact from "frmContactsListing".
The code from "frmContactsListing" looks as shown below:-
private void barButtonEditContact_ItemClick(object sender, ItemClickEventArgs e)
{
try{
var selectedContact = gridViewContacts.GetFocusedRow() as Contact;
frmEditContact editContact = new frmEditContact(selectedContact, _contactService);
editContact.ShowDialog();
}
catch (Exception Errhandler){
MessageBox.Show(Errhandler.Message);
}
}
And the code from "frmEditContact" looks as shown below:-
public frmEditContact(IContactService contactService) {
InitializeComponent();
_photoService = UnityConfig.GetContainer().Resolve<IContactPhotoService>();
_contactService = contactService;
}
public frmEditContact(Contact contact, IContactService contactService)
: this(contactService) {
this._contact = contact;
SelectedPhotoId = _contact.ContactId;
contactNameTextEdit.Text = _contact.ContactName;
physicalAddressTextEdit.Text = _contact.PhysicalAddress;
postalAddressTextEdit.Text = _contact.PostalAddress;
telephoneTextEdit.Text = _contact.Telephone;
photoBindingSource.DataSource = _photoService.GetById(_contact.ContactId);
byte[] byteImg = ImageToByteArray(contactPhotoPictureBox.Image);
}
private void simpleButtonAccept_Click(object sender, EventArgs e) {
try {
_contact.ContactName = contactNameTextEdit.Text;
_contact.PhysicalAddress = physicalAddressTextEdit.Text;
_contact.PostalAddress = postalAddressTextEdit.Text;
_contact.Telephone = telephoneTextEdit.Text;
contactBindingSource.EndEdit();
_contactService.Update(_contact);
byte[] byteImg = ImageToByteArray(contactPhotoPictureBox.Image);
var photoSelection = _photoService.GetById(SelectedPhotoId);
photoSelection.PhotoData = byteImg;
photoBindingSource.EndEdit();
_photoService.update(photoSelection);
} catch(Exception Errhandler) {
MessageBox.Show(Errhandler.Message);
}
}
public byte[] ImageToByteArray(Image img) {
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
The simpleButtonAccept_Click method accepts the edit.
Now, How do I refresh "frmContactsListing" once the edit form is closed to accomodate the edit changes,
taking into cosideration I have updated two binding sources, contactBindingSource and photoBindingSource. Entity framework.
Thanks.
Peter Dzuya.

Answers (2)