context: using System;
  using System.Globalization;
  using System.Collections;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using C1.Win.C1FlexGrid;
  using Biruni.Shared;
  using Biruni.Shared.Data;
  using Biruni.Shared.Data.dsCoreTableAdapters;
  using Biruni.Shared.Logging;
  using Biruni.Master.Search;
  
  namespace Biruni.Sales.Entry
  {
      public partial class frmSalesOrderEntry : Biruni.Shared.Templates.frmEntry2
      {
          int InvoiceID = -1;
          int PaymentID = -1;
          string InvoiceNum = String.Empty;
          string PaymentNum = String.Empty;
  
          public frmSalesOrderEntry()
          {
              frmSalesOrderEntry_Helper(-1);
          }
  
          public frmSalesOrderEntry(int id)
          {
              frmSalesOrderEntry_Helper(id);
          }
  
          public void frmSalesOrderEntry_Helper(int id)
          {
              InitializeComponent();
              InitializeForm();
              InitializeData(id);
          }
  
          protected override void OnShown(EventArgs e)
          {
              c1TextBox1.Focus();
              base.OnShown(e);
          }
  
          private void InitializeData(int id)
          {
              // main data
              try
              {
                  dsCore1.EnforceConstraints = false;
                  dsCore2.EnforceConstraints = false;
                  // set database connection
                  daCurrencies1.Connection = AppHelper.GetDbConnection();
                  daCompanies1.Connection = AppHelper.GetDbConnection();
                  daItems1.Connection = AppHelper.GetDbConnection();
                  daOrders1.Connection = AppHelper.GetDbConnection();
                  daOrderDetails1.Connection = AppHelper.GetDbConnection();
                  // lookup table
                  daCompanies1.FillCustomerActive(dsCore1.Companies);
                  daCurrencies1.FillActive(dsCore1.Currencies);
                  daOrders1.FillSalesQuotationAll(dsCore2.Orders);
                  // get data
                  if (id < 0)
                  {
                      // mode
                      txMode = DataEntryModes.Add;
                      // add new row to master table
                      BindingContext[dsCore1, "Orders"].AddNew();
                      // default values for master table
                      DataRowView dr = (DataRowView)this.BindingContext[dsCore1, "Orders"].Current;
                      dr["OrderNo"] = DbHelper.GenerateNewOrderID(ModulePrefix, DateTime.Today.Year);
                      dr["OrderValue"] = 0;
                      dr["OrderType"] = ModulePrefix;
                      dr["OrderDate"] = DateTime.Today;
                      dr["RequiredDate"] = DateTime.Today.AddDays(30);
                      dr["PaymentValue"] = 0;
                      dr["PaymentType"] = 1;  // credit payment
                  }
                  else
                  {
                      // mode
                      txMode = DataEntryModes.Edit;
                      // get data
                      daOrders1.ClearBeforeFill = true;
                      daOrders1.FillByID(dsCore1.Orders, id);
                      // get details
                      daOrderDetails1.ClearBeforeFill = true;
                      daOrderDetails1.FillByOrderID(dsCore1.OrderDetails, id);
                  }
              }
              catch (Exception ex)
              {
                  Logger.ErrorRoutine(ex);
                  RibbonMessageBox.Show("ERROR Loading Data: " + ex.Message,
                      Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
              }
          }
  
          private void InitializeForm()
          {
              AppHelper.ApplyVisualStyle(this.Controls);
              // transaction type
              ModulePrefix = TransactionTypes.TX_SALES_ORDER;
              // grid event handlers
              _grid.Enter += new EventHandler(_grid_Enter);
              _grid.AfterEdit += new RowColEventHandler(_grid_AfterEdit);
              _grid.GetUnboundValue += new UnboundValueEventHandler(_grid_GetUnboundValue);
              _grid.AfterAddRow += new RowColEventHandler(_grid_AfterAddRow);
              _grid.KeyDownEdit += new KeyEditEventHandler(_grid_KeyDownEdit);
              _grid.CellButtonClick += new RowColEventHandler(_grid_CellButtonClick);
              // grid column setting
              try
              {
                  // column headers
                  _grid.Cols["ID"].Caption = "ID";
                  _grid.Cols["OrderID"].Caption = "Order ID";
                  _grid.Cols["ItemID"].Caption = "Item ID";
                  _grid.Cols["ItemCode"].Caption = "Item Code";
                  _grid.Cols["ItemName"].Caption = "Item Name";
                  _grid.Cols["Quantity"].Caption = "Quantity";
                  _grid.Cols["MeasureCode"].Caption = "UoM";
                  _grid.Cols["MeasureName"].Caption = "Measure Name";
                  _grid.Cols["UnitPrice"].Caption = "Unit Price";
                  _grid.Cols["TrxType"].Caption = "Trx Type";
                  _grid.Cols["TaxPct"].Caption = "Tax (%)";
                  // read only columns
                  _grid.Cols["ID"].AllowEditing = false;
                  _grid.Cols["OrderID"].AllowEditing = false;
                  _grid.Cols["ItemID"].AllowEditing = false;
                  _grid.Cols["ItemName"].AllowEditing = false;
                  _grid.Cols["MeasureCode"].AllowEditing = false;
                  _grid.Cols["MeasureName"].AllowEditing = false;
                  // hide columns
                  _grid.Cols["ID"].Visible = false;
                  _grid.Cols["ItemID"].Visible = false;
                  _grid.Cols["OrderID"].Visible = false;
                  _grid.Cols["MeasureName"].Visible = false;
                  _grid.Cols["UnitPrice"].Visible = true;
                  _grid.Cols["TaxPct"].Visible = true;
                  _grid.Cols["TrxType"].Visible = true;
                  _grid.Cols["ReferenceID"].Visible = false;
                  _grid.Cols["ReferenceNo"].Visible = false;
                  _grid.Cols["ReferenceDate"].Visible = false;
                  _grid.Cols["ReferenceValue"].Visible = false;
                  // number format
                  _grid.Cols["ID"].Format = "N2";
                  _grid.Cols["OrderID"].Format = "N2";
                  _grid.Cols["ItemID"].Format = "N2";
                  _grid.Cols["Quantity"].Format = "N2";
                  _grid.Cols["UnitPrice"].Format = "N2";
                  _grid.Cols["TaxPct"].Format = "N2";
                  // COmbo option
                  _grid.Cols["ItemCode"].ComboList = "|...";
                  // column width
                  _grid.Cols["ID"].Width = -1;
                  _grid.Cols["OrderID"].Width = -1;
                  _grid.Cols["ItemID"].Width = -1;
                  _grid.Cols["ItemCode"].Width = 120;
                  _grid.Cols["ItemName"].Width = 150;
                  _grid.Cols["Quantity"].Width = 70;
                  _grid.Cols["MeasureCode"].Width = 50;
                  _grid.Cols["MeasureName"].Width = -1;
                  _grid.Cols["UnitPrice"].Width = 80;
                  _grid.Cols["TrxType"].Width = 70;
                  _grid.Cols["TaxPct"].Width = 50;
                  // transaction type
                  _grid.Cols["TrxType"].DataMap = AppHelper.TransactionTypeDataMap();
              }
              catch (Exception ex)
              {
                  Logger.ErrorRoutine(ex);
                  RibbonMessageBox.Show("ERROR Loading Data!\n" + ex.Message);
              }
          }
  
          #region FlexGrid Events
  
          private void _grid_CellButtonClick(object sender, RowColEventArgs e)
          {
              Cursor = Cursors.AppStarting;
              try
              {
                  // open item list form
                  if (e.Col == _grid.Cols["ItemCode"].SafeIndex)
                  {
                      frmItemSearch fx;
                      if (_grid[e.Row, "ItemCode"] == DBNull.Value)
                          fx = new frmItemSearch();
                      else
                          fx = new frmItemSearch((int)_grid[e.Row, "ItemID"]);
                      fx.ShowDialog();
  
                      if (fx.SelectedOK)
                      {
                          dsCore.ItemsDataTable data = daItems1.GetDataByID(fx.SelectedID);
                          // item info
                          if (data.Rows.Count > 0)
                          {
                              _grid.SetData(e.Row, "ItemID", data[0].ID);
                              _grid.SetData(e.Row, "ItemCode", data[0].Code);
                              _grid.SetData(e.Row, "ItemName", data[0].Name);
                              _grid.SetData(e.Row, "MeasureCode", data[0].IsMeasureCodeNull() ? "" : data[0].MeasureCode);
                              _grid.SetData(e.Row, "UnitPrice", data[0].IsSellingPriceNull() ? 0 : data[0].SellingPrice);
                              _grid.SetData(e.Row, "TaxPct", 0);
                              _grid.SetData(e.Row, "TrxType", 0);
                              _grid.SetData(e.Row, "Quantity", 1);
                              // display default remark
  
gt:                             _grid.SetData(e.Row, "Remarks", c1TextBox2.Text);