Imports Model
Imports Telerik.WinControls.UI
Imports System.ComponentModel
Imports System.ServiceModel
Imports WinFormClient.ServiceReference1


Public Class Form1

    ' reference to our WCF service client
    Private _client As New ServiceReference1.Service1Client()

    ' wrapper for OpenAccess Container and ChangeSets
    Private _containerHelper As New ContainerHelper()

    ' currently selected group name
    Private ReadOnly Property CurrentGroupName() As String
        Get
            Dim index As Integer = cbContactGroups.SelectedIndex
            If index <> -1 Then
                Return cbContactGroups.Items(index).Text
            End If
            Return [String].Empty
        End Get
    End Property
    ' load contact groups to combo
    Private Sub LoadCombo()
        ' get all contact groups as a ChangeSet from the service,
        ' load the ChangeSet to the container and return an
        ' IList of ContactGroup
        Dim contactGroups As IList(Of ContactGroup) = _
      _containerHelper.Load(Of ContactGroup)(_client.GetContactGroups())

        ' bind to the grid and display the ContactGroupName 
        ' in the combo
        cbContactGroups.DataSource = contactGroups
        cbContactGroups.DisplayMember = "ContactGroupName"
    End Sub

    Private Sub bindingList_RemovingItem(ByVal sender As Object, _
        ByVal args As RemovingItemEventArgs)
        _containerHelper.Remove(args.Item)
    End Sub
    ' create a new contact and add to the grid and to the container.
    ' Note: e.NewObject shows up in the 'add new row' before being added
    ' to the grid data.
    Private Sub bindingList_AddingNew( _
    ByVal sender As Object, _
    ByVal e As AddingNewEventArgs)
        Dim contactGroup As ContactGroup = _
      TryCast((TryCast(cbContactGroups.SelectedItem, RadComboBoxItem)).Tag,  _
      ContactGroup)

        e.NewObject = New Contact With { _
      .ContactName = "New " & _
      contactGroup.ContactGroupName, _
      .ContactGroup = contactGroup, _
      .Phone = String.Empty}
        _containerHelper.Add(e.NewObject)
    End Sub
    ' load all contacts for a group to grid
    Private Sub LoadGrid()
        ' get all Contacts as a ChangeSet from the service,
        ' load the ChangeSet to the container and return
        ' an IList of Contacts
        Dim contacts As IList(Of Contact) = _
      _containerHelper.Load(Of Contact)(_client.GetContacts())

        ' filter the contacts for only those with the currently selected
        ' group name
        Dim filteredContacts As List( _
      Of Contact) = contacts.Where(Function(c) _
      c.ContactGroup.ContactGroupName.Equals(Me.CurrentGroupName)).ToList()

        Dim bindingList As RemovableBindingList(Of Contact) = _
      New RemovableBindingList(Of Contact)(filteredContacts)
        AddHandler bindingList.AddingNew, AddressOf bindingList_AddingNew
        AddHandler bindingList.RemovingItem, AddressOf bindingList_RemovingItem

        ' bind to the grid
        gvContacts.DataSource = bindingList

        ' hide the contact group column and fit the remaining columns
        If gvContacts.MasterGridViewTemplate.Columns.Count > 1 Then
            gvContacts.MasterGridViewTemplate.Columns(2).IsVisible = False
        End If
        gvContacts.MasterGridViewTemplate.BestFitColumns()
    End Sub
    Private Sub cbContactGroups_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbContactGroups.SelectedIndexChanged
        LoadGrid()
    End Sub
    Private Sub cbContactGroups_ItemDataBound(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.ItemDataBoundEventArgs) Handles cbContactGroups.ItemDataBound
        e.DataBoundItem.Tag = TryCast(e.DataItem, ContactGroup)
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            ' pass SaveContacts as a delegate
            _containerHelper.Save(AddressOf _client.SaveContacts)
            ' Catch  FaultException ex As FaultException(Of ContactFault)
        Catch ex As FaultException(Of ContactFault)
            ' construct an error message that lists the invalid
            ' contact names
            Const format As String = "{0}. Rolling back changes for ""{1}"""
            Dim contactNames As String = _
         String.Join(",", (TryCast(ex.Detail, ContactFault)).ContactNames)
            Dim message As String = _
         String.Format(format, ex.Reason.GetMatchingTranslation().Text, _
         contactNames)

            ' use the FaultException to get the reason, code, subcode and respond.
            MessageBox.Show(ex.Reason.GetMatchingTranslation().Text & _
          ". Rolling back changes for " & contactNames)

            ' remove 'new' Contact objects and reload grid
            _containerHelper.Reset(Of Contact)()
            LoadGrid()
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadCombo()
        LoadGrid()
    End Sub
End Class
