using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _2_ORM_Model;
using Telerik.OpenAccess;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace _2_ORM_MyWinFormAppRad
{
    public partial class Form1 : Telerik.WinControls.UI.RadForm
    {
        private IObjectScope _scope = NorthwindProvider.ObjectScope();
        public Form1()
        {
            InitializeComponent();

            // hide the "Add new row" button
            gvProducts.MasterGridViewTemplate.AllowAddNewRow = false;
            // SelectedIndex is already "0", so work around by setting to 
            // "no selection", i.e. "-1", then first item "0"
            cbCategories.SelectedIndex = -1;
            cbCategories.SelectedIndex = 0;
        }

        private void cbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((sender as RadComboBox).SelectedValue != null)
            {
                IObjectScope scope = NorthwindProvider.ObjectScope();

                int? selectValue = (int?)(sender as RadComboBox).SelectedValue;
                var filteredData = from product in scope.Extent<Product>()
                                   where product.CategoryID == selectValue
                                   select new
                                   {
                                       Product = product.ProductName,
                                       QuantityPerUnit = product.QuantityPerUnit,
                                       Category = product.Category.CategoryName
                                   };

                gvProducts.DataSource = filteredData.ToList();
                gvProducts.MasterGridViewTemplate.BestFitColumns();
            }

        }

    }
}
