using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using C1.Data;

namespace ClientApp
{
    /// <summary>
    /// Summary description for ViewOrderForm.
    /// </summary>
    public class ViewOrderForm : System.Windows.Forms.Form
    {
        private C1.Data.C1DataSet c1DataSet1;
        private System.Windows.Forms.Label label1;
        private C1.Data.C1DataView c1DataView1;
        private System.Windows.Forms.Button buttonOK;
        private System.Windows.Forms.Button buttonCancel;
        private C1.Win.C1TrueDBGrid.C1TrueDBGrid gridItems;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ViewOrderForm()
        {
            //
            // 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.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ViewOrderForm));
            this.c1DataSet1 = new C1.Data.C1DataSet();
            this.buttonOK = new System.Windows.Forms.Button();
            this.buttonCancel = new System.Windows.Forms.Button();
            this.gridItems = new C1.Win.C1TrueDBGrid.C1TrueDBGrid();
            this.c1DataView1 = new C1.Data.C1DataView();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridItems)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataView1)).BeginInit();
            this.SuspendLayout();
            // 
            // c1DataSet1
            // 
            this.c1DataSet1.BindingContextCtrl = null;
            this.c1DataSet1.DataLibrary = "DataLibrary1";
            this.c1DataSet1.DataLibraryUrl = "";
            this.c1DataSet1.DataSetDef = "DataSet";
            this.c1DataSet1.Locale = new System.Globalization.CultureInfo("en-US");
            this.c1DataSet1.SchemaClassName = "DataLibrary1.DataClass";
            this.c1DataSet1.SchemaDef = null;
            this.c1DataSet1.SuppressNotifications = false;
            this.c1DataSet1.UpdateError += new C1.Data.DataSetEventHandler(this.c1DataSet1_UpdateError);
            // 
            // buttonOK
            // 
            this.buttonOK.Location = new System.Drawing.Point(272, 240);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(152, 23);
            this.buttonOK.TabIndex = 0;
            this.buttonOK.Text = "Update database now";
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            // 
            // buttonCancel
            // 
            this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.buttonCancel.Location = new System.Drawing.Point(432, 240);
            this.buttonCancel.Name = "buttonCancel";
            this.buttonCancel.TabIndex = 1;
            this.buttonCancel.Text = "Close";
            this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
            // 
            // gridItems
            // 
            this.gridItems.AllowFilter = true;
            this.gridItems.AllowRowSizing = C1.Win.C1TrueDBGrid.RowSizingEnum.AllRows;
            this.gridItems.AllowSort = true;
            this.gridItems.CaptionHeight = 17;
            this.gridItems.CollapseColor = System.Drawing.Color.Black;
            this.gridItems.DataChanged = false;
            this.gridItems.DataSource = this.c1DataView1;
            this.gridItems.BackColor = System.Drawing.Color.Empty;
            this.gridItems.ExpandColor = System.Drawing.Color.Black;
            this.gridItems.Images.Add(((System.Drawing.Bitmap)(resources.GetObject("resource.Images"))));
            this.gridItems.Location = new System.Drawing.Point(8, 24);
            this.gridItems.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.DottedCellBorder;
            this.gridItems.Name = "gridItems";
            this.gridItems.PreviewInfo.Location = new System.Drawing.Point(0, 0);
            this.gridItems.PreviewInfo.Size = new System.Drawing.Size(0, 0);
            this.gridItems.PreviewInfo.ZoomFactor = 75;
            this.gridItems.RecordSelectorWidth = 16;
            this.gridItems.RowHeight = 15;
            this.gridItems.RowSubDividerColor = System.Drawing.Color.DarkGray;
            this.gridItems.ScrollTips = false;
            this.gridItems.Size = new System.Drawing.Size(496, 200);
            this.gridItems.TabIndex = 2;
            this.gridItems.Text = "c1TrueDBGrid1";
            this.gridItems.PropBag = ((string)(resources.GetObject("gridItems.PropBag")));
            // 
            // c1DataView1
            // 
            this.c1DataView1.BindingContextCtrl = null;
            this.c1DataView1.DataSet = this.c1DataSet1;
            this.c1DataView1.TableName = "";
            this.c1DataView1.TableViewName = "Order Details";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.TabIndex = 3;
            this.label1.Text = "Order items:";
            // 
            // ViewOrderForm
            // 
            this.AcceptButton = this.buttonOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(512, 277);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.gridItems,
                                                                          this.label1,
                                                                          this.buttonCancel,
                                                                          this.buttonOK});
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ViewOrderForm";
            this.Text = "View/Edit order items";
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridItems)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataView1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void buttonOK_Click(object sender, System.EventArgs e)
        {
            c1DataSet1.Update();
        }

        int _orderId = -1;
        internal int OrderID
        {
            get { return _orderId; }
            set
            {
                _orderId = value;
                c1DataView1.RowFilter = "OrderID = " + _orderId;
            }
        }

        private void c1DataSet1_UpdateError(object sender, C1.Data.DataSetEventArgs e)
        {
            // Handling Update errors on the client
            if (e.Error == null)  // e.Error == null means concurrency conflict
            {
                UnitPriceForm form = new UnitPriceForm();

                C1DataRow[] rows = c1DataSet1.Tables["Order Details"].GetErrors();

                if (rows.Length == 0)
                    return;

                // To simplify the sample, we assume that conflict occured in a single row

                int orderID = (int)rows[0]["OrderID"];
                int productID = (int)rows[0]["ProductID"];
                object[] values = rows[0].ItemArray;

                form.numericNew.Value = (decimal)rows[0]["UnitPrice"];
                c1DataSet1.Fill();
                object[] key = new object[2];
                key[0] = orderID;
                key[1] = productID;
                C1DataRow row = c1DataSet1.Tables["Order Details"].Rows.Find(key);

                if (row == null)
                    return;

                form.numericStored.Value = (decimal)row["UnitPrice"];
                DialogResult res = form.ShowDialog();
                if (res == DialogResult.Retry)  // the user specified a new UnitPrice value, try again
                {
                    row.ItemArray = values;
                    row["UnitPrice"] = form.numericNew.Value;
                    e.Status = DataSetEventStatusEnum.Continue;
                }
                else if (res == DialogResult.Ignore)  // accept all changes except the UnitPrice value
                {
                    row.ItemArray = values;
                    row["UnitPrice"] = form.numericStored.Value;
                    e.Status = DataSetEventStatusEnum.Continue;
                }
                else
                    e.Status = DataSetEventStatusEnum.Skip; // cancel the update

            }
        }

        private void buttonCancel_Click(object sender, System.EventArgs e)
        {
            Close();
        }
    }
}
