
Imports Telerik.OpenAccess
Imports Telerik.WinControls.UI
Imports Telerik.WinControls

Public Class RadForm1

    Private _scope As IObjectScope = NorthwindProvider.ObjectScope()

    Public Sub New()
        InitializeComponent()

        cbCategories.SelectedIndex = -1
        cbCategories.SelectedIndex = 0

        _scope.TransactionProperties.AutomaticBegin = True
        gvProducts.MasterGridViewTemplate.AutoSizeColumnsMode = _
           GridViewAutoSizeColumnsMode.Fill
    End Sub

    Private Sub cbCategories_SelectedIndexChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles cbCategories.SelectedIndexChanged

        If (TryCast(sender, RadComboBox)).SelectedValue <> Nothing Then

            Dim selectValue As System.Nullable(Of Integer) = _
      DirectCast((TryCast(sender, RadComboBox)).SelectedValue,  _
      System.Nullable(Of Integer))
            ' select only products for this category and assign to the 
            ' ObjectView DataSource
            opProduct.ObjectSource = From product In _scope.Extent(Of Product)() _
               Where product.CategoryID = selectValue _
               Select product

            ' display only these two columns
            gvProducts.Columns.Clear()
            gvProducts.Columns.Add(New GridViewTextBoxColumn("CategoryID"))
            gvProducts.Columns.Add(New GridViewTextBoxColumn("ProductName"))
            gvProducts.Columns.Add(New GridViewTextBoxColumn("QuantityPerUnit"))
        End If
    End Sub

    Private Sub bntClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntClose.Click
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _scope.Transaction.Rollback()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        opProduct.SaveAll()
        _scope.Transaction.Commit()
    End Sub

    Private Sub RadForm1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        RadMessageBox.SetThemeName(Me.ThemeName)

        If _scope.Transaction.IsDirty Then
            If RadMessageBox.Show("Save all changes?", _
      "Pending Changes", MessageBoxButtons.OKCancel) _
       = DialogResult.OK Then
                opProduct.SaveAll()
                _scope.Transaction.Commit()
            Else
                _scope.Transaction.Rollback()
            End If
        End If
    End Sub

End Class