using System;
using Model;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Drawing;
using System.ComponentModel;

namespace MyWinFormApp
{
  public partial class Form1 : RadForm
  {
    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;

      // just for fun, set the color of the form background
      this.FormElement.ClientFillPrimitive.BackColor =
        Color.FromArgb(159, 159, 159);
      this.FormElement.ClientFillPrimitive.BackColor2 =
        Color.FromArgb(120, 120, 120);
      this.FormElement.ClientFillPrimitive.BackColor3 =
        Color.FromArgb(110, 110, 110);
      this.FormElement.ClientFillPrimitive.GradientStyle =
        GradientStyles.OfficeGlassRect;
    }

    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();
      }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
  }
}
