using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace Tutor05
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private C1.Data.Express.C1ExpressTable c1ExpressTable1;
        private System.Windows.Forms.DataGrid dataGrid1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.c1ExpressTable1 = new C1.Data.Express.C1ExpressTable();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            ((System.ComponentModel.ISupportInitialize)(this.c1ExpressTable1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // c1ExpressTable1
            // 
            this.c1ExpressTable1.BindingContextCtrl = this;
            this.c1ExpressTable1.ConnectionComponent = null;
            this.c1ExpressTable1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\ComponentOne Studio" +
                ".NET 2.0\\common\\Nwind.mdb;Persist Security Info=False";
            this.c1ExpressTable1.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("c1ExpressTable1.DummyImage")));
            this.c1ExpressTable1.FullComponentName = "Form1.c1ExpressTable1";
            this.c1ExpressTable1.SchemaTableName = "Order Details";
            this.c1ExpressTable1.TableViewName = "Order_Details";
            this.c1ExpressTable1.BeforeFill += new C1.Data.FillEventHandler(this.c1ExpressTable1_BeforeFill);
            this.c1ExpressTable1.BeforeFieldChange += new C1.Data.FieldChangeEventHandler(this.c1ExpressTable1_BeforeFieldChange);
            this.c1ExpressTable1.BeforeEndEdit += new C1.Data.RowChangeEventHandler(this.c1ExpressTable1_BeforeEndEdit);
            this.c1ExpressTable1.SerializedSchemaXML = this.c1ExpressTable1.GetSchemaFromResources(resources, "c1ExpressTable1", this);
            // 
            // dataGrid1
            // 
            this.dataGrid1.DataMember = "";
            this.dataGrid1.DataSource = this.c1ExpressTable1;
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, 8);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(536, 304);
            this.dataGrid1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(560, 333);
            this.Controls.Add(this.dataGrid1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.c1ExpressTable1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private void c1ExpressTable1_BeforeFieldChange(object sender, C1.Data.FieldChangeEventArgs e)
        {
            if (e.Field.Name == "Quantity")
            {
                if ((short)e.NewValue < 1)
                    e.NewValue = (short)1;
            }
            else if (e.Field.Name == "Discount")
            {
                if ((float)e.NewValue < 0 || (float)e.NewValue > 1)
                    throw new ApplicationException("Discount must be between 0 and 1");
            }
        }

        private void c1ExpressTable1_BeforeEndEdit(object sender, C1.Data.RowChangeEventArgs e)
        {
            if ((short)e.Row["Quantity"] * (decimal)e.Row["UnitPrice"] > 100000)
                throw new ApplicationException("Too expensive");
        }

        #region Switch to C1NWind.mdb database

        private void c1ExpressTable1_BeforeFill(object sender, C1.Data.FillEventArgs e)
        {
            if (!c1ExpressTable1.ExpressConnection.DataSet.Filled)
            {
                UpdateDbPath();
            }
        }

        private void UpdateDbPath()
        {
            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (File.Exists(dbPath))
            {
                OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder(
                    c1ExpressTable1.ConnectionString);
                csb.DataSource = dbPath;
                c1ExpressTable1.ConnectionString = csb.ToString();
            }
        }

        #endregion
    }
}
