The logic in the calling form is as follows:
Private Sub AddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewItem.Click
Dim editfrm As New GL_GroupEdit
editfrm.LoadGroup(9999)
editfrm.ShowDialog(Me)
If editfrm.cancelClicked Then
Return
End If
resetGrid()
The edit form has two text boxes, they are both bound to a glgroupcollection. The LoadGroup logic looks like this:
Friend Sub LoadGroup(ByVal GroupNumber As Int16)
Dim filter As New PredicateExpression(GlgroupFields.GroupId = GroupNumber)
GlgroupCollection1.GetMulti(filter)
End Sub
It also has a save and a cancel button. The save logic looks like this:
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
cancelClicked = False
Dim groupToSave As GlgroupEntity = DirectCast(GLGroupBindingSource.Current, GlgroupEntity)
If GetEntityFieldsErrors() <> String.Empty Then
MessageBox.Show("There are errors in the group. Please fix them prior to save.", "Please fix the errors.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
Else
Try
groupToSave.Save(True)
Catch ex As ORMEntityValidationException
MessageBox.Show(ex.Message)
Exit Sub
End Try
End If
Me.Close()
End Sub
Here's the GetEntityFieldsErrors logic:
Private Function GetEntityFieldsErrors() As String
Dim currentItem As GlgroupEntity = DirectCast(GLGroupBindingSource.Current, GlgroupEntity)
Dim sbErrors As New StringBuilder
Dim toReturn As String = String.Empty
For Each field As IEntityField In currentItem.Fields
If Not String.IsNullOrEmpty(DirectCast(currentItem, System.ComponentModel.IDataErrorInfo)(field.Name)) Then
sbErrors.Append(DirectCast(currentItem, System.ComponentModel.IDataErrorInfo)(field.Name) & ";")
End If
Next
If sbErrors.ToString() <> String.Empty Then
toReturn = sbErrors.ToString()
toReturn = toReturn.Substring(0, toReturn.Length - 2)
End If
Return (toReturn)
End Function