not sure if this will help, but see if changing your btn_Save to be like this and then add the GetControlBoundToMember method as well, I am guessing your btnSave method looks similar to the one below. You will also need to add an ErrorProvider Control to your form and call it err1 and move any controls that might be in a groupbox out of the groupbo and place them onto the form, unless you create a recursive method that search through controls that have a collect of controls.
private void btnSave_Click(object sender, EventArgs e)
{
if (_entity != null)
{
try
{
_service.Save(_entity.UpdateEntity());
}
catch (InvalidOperationException ex)
{
//do something here to display errors
listBox1.Items.Clear();
foreach (var r in _entity.Errors)
{
listBox1.Items.Add(r.ErrorMessage);
foreach (var c in GetControlBoundToMember(r.MemberNames.ToList()))
{
err1.SetError(c, r.ErrorMessage);
}
}
}
}
}
private IList<Control> GetControlBoundToMember(IList<string> memberNames)
{
List<Control> controls = new List<Control>();
foreach (Control control in this.Controls)
{
foreach (var mn in memberNames)
{
foreach (Binding binding in control.DataBindings)
{
if (binding.BindingMemberInfo.BindingField == mn) controls.Add(control);
}
}
}
return controls;
}
AB