using System;
using System.Collections.Generic;
using System.Windows.Forms;
using _1_Model;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;
using Telerik.WinControls.UI;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private OpenAccessTracer tracer = null;

    private void Form1_Load(object sender, EventArgs e)
    {

      // instantiate an OpenAccessTracer to provide
      // DB traffic feedback
      tracer = new OpenAccessTracer(tbStatus, lblStatus);

      // get a new object scope
      IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();

      // setup the fetch plan and add the "ContactFetch" group
      scope.FetchPlan.Clear();
      //scope.FetchPlan.Add(FetchPlan.Default); 
      scope.FetchPlan.Add("ContactFetch");
      scope.FetchPlan.MaxDepth = 3; 
      //scope.FetchPlan = FetchPlans.ContactFetchPlan;

      // populate persistent objects from the database
      List<Contact> list = scope.Extent<Contact>().ToList();

      // configure and bind the grid to the Contact.ContactName and
      // Contact.ContactGroup.ContactGroupName properties.
      radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;
      radGridView1.ReadOnly = true;
      radGridView1.Columns.Add(new GridViewTextBoxColumn("ContactName"));
      radGridView1.Columns.Add(new GridViewTextBoxColumn("ContactGroup.ContactGroupName"));
      radGridView1.Columns[0].HeaderText = "Contact";
      radGridView1.Columns[1].HeaderText = "Group";
      radGridView1.DataSource = list;
      radGridView1.MasterGridViewTemplate.BestFitColumns();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
      tracer.Reset();
    }
  }
}
