Imports TutorialsSilverlight.Web

Public Class ProductInfo
    Public Overridable Property ProductID as Integer
    Public Overridable Property ProductName as String
    Public Overridable Property CategoryID as Integer?
    Public Overridable Property CategoryName as String
    Public Overridable Property UnitPrice as Decimal?
    Public Overridable Property QuantityPerUnit as String
    Public Overridable Property UnitsInStock as Short?
    Public Overridable Property UnitsOnOrder as Short?
End Class

Partial Public Class CustomColumns
    Inherits UserControl

    Public Sub New()
        InitializeComponent()

        ' Define a live view with custom fields.
        ' The data grid will automatically generate columns based on these fields.
        dataGrid1.ItemsSource = _
            (From p In c1DataSource1("GetProducts").AsLive(Of Product)()
             Select New ProductInfo With
             {
                 .ProductID = p.ProductID,
                 .ProductName = p.ProductName,
                 .CategoryID = p.CategoryID,
                 .CategoryName = p.Category.CategoryName,
                 .UnitPrice = p.UnitPrice,
                 .QuantityPerUnit = p.QuantityPerUnit,
                 .UnitsInStock = p.UnitsInStock,
                 .UnitsOnOrder = p.UnitsOnOrder
             })
    End Sub

    Private Sub addItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles addItem.Click
        ' add a new product
        Dim newItem = CType(dataGrid1.ItemsSource, System.ComponentModel.IEditableCollectionView).AddNew()
        dataGrid1.ScrollIntoView(newItem, dataGrid1.Columns(0))
    End Sub

    Private Sub deleteItem_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles deleteItem.Click
        ' delete the current product if it exists
        Dim item = CType(dataGrid1.ItemsSource, System.ComponentModel.ICollectionView).CurrentItem
        If item IsNot Nothing Then
            CType(dataGrid1.ItemsSource, System.ComponentModel.IEditableCollectionView).Remove(item)
        End If
    End Sub
End Class
