/// <summary>
        /// Sales grid cell leave event to calculate the basic functions and calculation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvSalesInvoice_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (IsSetGridValueChange == true)
            {
                BatchBll BllBatch = new BatchBll();

                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
               List< DataTable > listUnitConversion = new List<DataTable>();
                decimal decBatchId = 0;
                string strBarcode = string.Empty;
                try
                {
                    if (e.RowIndex != -1 && e.ColumnIndex != -1)
                    {
                        if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductName"].Value != null)
                        {
                            if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductName"].Value.ToString() != string.Empty)
                            {
                                if (dgvSalesInvoice.RowCount > 1)
                                {
                                    if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value != null)
                                    {
                                        try
                                        {
                                            if (decimal.Parse(dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceDeliveryNoteDetailsId"].Value.ToString()) > 0)
                                            {
                                                if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value != null)
                                                {
                                                    if (decimal.Parse(dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value.ToString()) > decimal.Parse(dtblDeliveryNoteDetails.Rows[e.RowIndex]["qty"].ToString()))
                                                    {
                                                        dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value = dtblDeliveryNoteDetails.Rows[e.RowIndex]["qty"].ToString();
                                                        if (decDeliveryNoteQty < decimal.Parse(dtblDeliveryNoteDetails.Rows[e.RowIndex]["qty"].ToString()))
                                                        {
                                                            dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value = decDeliveryNoteQty;
                                                            decDeliveryNoteQty = 0;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                    if (dgvSalesInvoice.Columns[e.ColumnIndex].Name == "dgvtxtSalesInvoicembUnitName")
                                    {
                                        if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoicembUnitName"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoicembUnitName"].Value.ToString() != string.Empty)
                                        {
                                            UnitConversionCalc(e.RowIndex);
                                        }
                                    }
                                    if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceDiscountPercentage"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceDiscountPercentage"].Value.ToString() != string.Empty || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceDiscountAmount"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceDiscountAmount"].Value.ToString() != string.Empty
                                        || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value.ToString() != string.Empty || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductName"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductName"].Value.ToString() != string.Empty
                                        || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceRate"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceRate"].Value.ToString() != string.Empty || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductName"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductName"].Value.ToString() != string.Empty
                                       || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceGrossValue"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceGrossValue"].Value.ToString() != string.Empty || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value.ToString() != string.Empty)
                                    {
                                        DiscountCalculation(e.RowIndex, e.ColumnIndex);
                                    }
                                    if (dgvSalesInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvcmbSalesInvoiceTaxName" || dgvSalesInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtSalesInvoiceGrossValue" || dgvSalesInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtSalesInvoiceDiscountPercentage")
                                    {
                                        if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvcmbSalesInvoiceTaxName"].Visible)
                                        {
                                            if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvcmbSalesInvoiceTaxName"].Value != null && (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceNetAmount"].Value != null || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceGrossValue"].Value != null))
                                            {
                                                if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvcmbSalesInvoiceTaxName"].Value.ToString() != string.Empty && (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceNetAmount"].Value.ToString() != string.Empty || dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceGrossValue"].Value.ToString() != string.Empty))
                                                {
                                                    {
                                                        taxAndGridTotalAmountCalculation(e.RowIndex);
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            taxAndGridTotalAmountCalculation(e.RowIndex);
                                        }
                                    }
                                    if (dgvSalesInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtSalesInvoiceRate" || dgvSalesInvoice.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtSalesInvoiceQty")
                                    {
                                        if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceRate"].Value != null)
                                        {
                                            if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceQty"].Value.ToString() != string.Empty && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceRate"].Value.ToString() != string.Empty)
                                            {
                                                {
                                                    GrossValueCalculation(e.RowIndex);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (e.ColumnIndex == dgvSalesInvoice.Columns["dgvcmbSalesInvoiceBatch"].Index)
                                {
                                    if (dgvSalesInvoice.CurrentRow.Cells["dgvcmbSalesInvoiceBatch"].Value != null)
                                    {
                                        if (dgvSalesInvoice.CurrentRow.Cells["dgvcmbSalesInvoiceBatch"].Value.ToString() != string.Empty && dgvSalesInvoice.CurrentRow.Cells["dgvcmbSalesInvoiceBatch"].Value.ToString() != "0")
                                        {
                                            decBatchId = Convert.ToDecimal(dgvSalesInvoice.CurrentRow.Cells["dgvcmbSalesInvoiceBatch"].Value.ToString());
                                            strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                                            dgvSalesInvoice.CurrentRow.Cells["dgvtxtSalesInvoiceBarcode"].Value = strBarcode;
                                            if (dgvSalesInvoice.CurrentRow.Cells["dgvtxtSalesInvoiceProductId"].Value != null)      // here get product rate for sales(standard rate)
                                            {
                                                getProductRate(e.RowIndex, Convert.ToDecimal(dgvSalesInvoice.CurrentRow.Cells["dgvtxtSalesInvoiceProductId"].Value.ToString()), decBatchId);
                                                UnitConversionCalc(e.RowIndex);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductId"].Value != null && dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceProductId"].Value.ToString() != string.Empty)
                        {
                            if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceConversionRate"].Value != null)
                            {
                                decCurrentConversionRate = Convert.ToDecimal(dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceConversionRate"].Value.ToString());
                            }
                            if (dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceRate"].Value != null)
                            {
                                decCurrentRate = Convert.ToDecimal(dgvSalesInvoice.Rows[e.RowIndex].Cells["dgvtxtSalesInvoiceRate"].Value.ToString());
                            }
                        }
                        CheckInvalidEntries(e);
                        SiGridTotalAmountCalculation();
                        CalculateAmountFromGrid(e.RowIndex, e, e.ColumnIndex);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("SI: 105" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
 /// <summary>
 /// Grid CellEndEdit for product details fill to curresponding row in grid
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvPurchaseReturn_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 {
     try
     {
         string strBarcode2 = string.Empty;
         decimal decBatchId2 = 0;
         BatchBll BllBatch = new BatchBll();
         RackBll BllRack = new RackBll();
         PurchaseInvoiceBll BllPurchaseInvoice = new PurchaseInvoiceBll();
         PurchaseReturnBll BllPurchaseReturn = new PurchaseReturnBll();
         ProductCreationBll BllProductCreation = new ProductCreationBll();
         if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtproductName")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value.ToString().Trim() != string.Empty)
             {
                 ProductInfo infoProduct = BllProductCreation.ProductViewByName(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value.ToString());
                 if (infoProduct.ProductCode != null && infoProduct.ProductCode != string.Empty)
                 {
                     FillProductDetails(infoProduct.ProductCode.ToString(), e.RowIndex);
                 }
                 else
                 {
                     StringEmptyDetailsInGrid();
                 }
             }
             else
             {
                 dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductName"].Value = string.Empty;
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtproductCode")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value.ToString().Trim() != string.Empty)
             {
                 ProductInfo infoProduct = BllProductCreation.ProductViewByCode(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value.ToString());
                 if (infoProduct.ProductName != null && infoProduct.ProductCode != string.Empty)
                 {
                     FillProductDetails(infoProduct.ProductCode.ToString(), e.RowIndex);
                 }
                 else
                 {
                     StringEmptyDetailsInGrid();
                 }
             }
             else
             {
                 dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value = string.Empty;
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtbarcode")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value.ToString().Trim() != string.Empty)
             {
                 string strBarcode = dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value.ToString();
                 decBatchId = BllProductCreation.BatchIdByPartNoOrBarcode(strBarcode, strBarcode);
                 if (decBatchId > 0)
                 {
                     List<DataTable> listObjBatchName = new List<DataTable>();
                     listObjBatchName = BllProductCreation.ProductCodeAndBarcodeByBatchId(decBatchId);
                     dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtproductCode"].Value = listObjBatchName[0].Rows[0]["productCode"].ToString();
                     if (listObjBatchName[0].Rows[0]["barcode"].ToString() != null && listObjBatchName[0].Rows[0]["barcode"].ToString() != string.Empty)
                     {
                         dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value = listObjBatchName[0].Rows[0]["barcode"].ToString();
                     }
                     decimal batchId = BllBatch.BatchViewByBarcode(strBarcode);
                     dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = batchId;
                     FillProductDetails(listObjBatchName[0].Rows[0]["productCode"].ToString(), e.RowIndex);
                 }
                 else
                 {
                     StringEmptyDetailsInGrid();
                 }
             }
             else
             {
                 dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtbarcode"].Value = string.Empty;
             }
         }
         if (e.ColumnIndex == dgvPurchaseReturn.Columns["dgvcmbUnit"].Index)
         {
             if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
             {
                 if ((dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value != null) && (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() != string.Empty))
                 {
                     UnitConversionCalc(e.RowIndex);
                     AmountCalculation("dgvtxtqty", e.RowIndex);
                 }
             }
         }
         if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtqty" && isAmountcalc)
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString() != string.Empty && Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString()) != 0)
             {
                 if (cmbInvoiceNo.SelectedValue == null || cmbInvoiceNo.SelectedValue.ToString() == string.Empty)
                 {
                     AmountCalculation("dgvtxtqty", e.RowIndex);
                 }
                 else
                 {
                     List<DataTable> ListObj= BllPurchaseInvoice.PurchaseDetailsViewByPurchaseMasterIdWithRemaining(Convert.ToDecimal(cmbInvoiceNo.SelectedValue.ToString()), decPurchaseReturnMasterId, decPurchaseReturnVoucherTypeId);
                     if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtPurchaseDetailsId"].Value != null)
                     {
                         if (Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtPurchaseDetailsId"].Value.ToString()) > 0)
                         {
                             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value != null)
                             {
                                 if (Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString()) > Convert.ToDecimal(ListObj[0].Rows[e.RowIndex]["qty"].ToString()))
                                 {
                                     dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value = Math.Round(Convert.ToDecimal(ListObj[0].Rows[e.RowIndex]["qty"].ToString()), PublicVariables._inNoOfDecimalPlaces);
                                 }
                             }
                         }
                     }
                     AmountCalculation("dgvtxtqty", e.RowIndex);
                 }
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtrate" && isAmountcalc)
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString() != string.Empty && Convert.ToDecimal(dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtqty"].Value.ToString()) != 0)
             {
                 AmountCalculation("dgvtxtrate", e.RowIndex);
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvtxtdiscount")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtdiscount"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtdiscount"].Value.ToString().Trim() != string.Empty && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvtxtDiscount"].Value.ToString().Trim() != "0")
             {
                 AmountCalculation("dgvtxtrate", e.RowIndex);
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvcmbTax" && isAmountcalc)
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbTax"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbTax"].Value.ToString() != string.Empty)
             {
                 AmountCalculation("dgvtxtrate", e.RowIndex);
             }
         }
         else if (dgvPurchaseReturn.Columns[e.ColumnIndex].Name == "dgvcmbBatch")
         {
             if (dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null && dgvPurchaseReturn.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value.ToString() != string.Empty)
             {
                 decBatchId2 = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvcmbBatch"].Value);
                 strBarcode2 = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId2);
                 dgvPurchaseReturn.CurrentRow.Cells["dgvtxtbarcode"].Value = strBarcode2;
             }
         }
         CheckInvalidEntries(e);
     }
     catch (Exception ex)
     {
         MessageBox.Show("PR:80" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 private void dgvPhysicalStock_CellLeave(object sender, DataGridViewCellEventArgs e)
 {
     if (!isFromEditMode)
     {
         try
         {
             string strBarcode = string.Empty;
             string strProductCode = string.Empty;
             ProductInfo infoProduct = new ProductInfo();
             ProductCreationBll BllProductCreation = new ProductCreationBll();
             BatchBll BllBatch = new BatchBll();
             //PhysicalStockMasterSP spPhysicalStockMaster = new PhysicalStockMasterSP();
             PhysicalStockBll BllPhysicalStock = new PhysicalStockBll();
             if (e.RowIndex != -1 && e.ColumnIndex != -1)
             {
                 if (dgvPhysicalStock.Columns[e.ColumnIndex].Name == "dgvcmbBatch")
                 {
                     if (dgvPhysicalStock.CurrentRow.Cells["dgvcmbBatch"].Value != null)
                     {
                         if (Convert.ToString(dgvPhysicalStock.CurrentRow.Cells["dgvcmbBatch"].Value) != string.Empty &&
                            Convert.ToDecimal(dgvPhysicalStock.CurrentRow.Cells["dgvcmbBatch"].Value) != 0)
                         {
                             if (isGridValueChanged)
                             {
                                 decBatchId = Convert.ToDecimal(dgvPhysicalStock.CurrentRow.Cells["dgvcmbBatch"].Value);
                                 strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                                 isGridValueChanged = false;
                                 dgvPhysicalStock.CurrentRow.Cells["dgvtxtBarcode"].Value = strBarcode;
                                 isGridValueChanged = true;
                             }
                         }
                     }
                 }
                 CheckColumnMissing();
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show("PS:45" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
     }
 }
        /// <summary>
        /// dgvProduction grid cell valuechange event to calculate the basic functions and calculation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProduction_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (IsSetGridValueChange)
                {
                    if (e.RowIndex != -1 && e.ColumnIndex != -1)
                    {
                        if (dgvProduction.RowCount > 1)
                        {
                            if (dgvProduction.Columns[e.ColumnIndex].Name == "dgvcmbProductionunitId")
                            {
                                if (dgvProduction.Rows[e.RowIndex].Cells["dgvcmbProductionunitId"].Value != null && dgvProduction.Rows[e.RowIndex].Cells["dgvcmbProductionunitId"].Value.ToString() != string.Empty)
                                {
                                    UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                                    List<DataTable> listUnitByProduct = new List<DataTable>();
                                    listUnitByProduct = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduction.Rows[e.RowIndex].Cells["dgvtxtProductionProductId"].Value.ToString());
                                    foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                                    {
                                        if (dgvProduction.Rows[e.RowIndex].Cells["dgvcmbProductionunitId"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                                        {
                                            dgvProduction.Rows[e.RowIndex].Cells["dgvtxtProductionunitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                                            dgvProduction.Rows[e.RowIndex].Cells["dgvtxtProductionConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());

                                            decimal decNewConversionRate = Convert.ToDecimal(dgvProduction.Rows[e.RowIndex].Cells["dgvtxtProductionConversionRate"].Value.ToString());
                                            decimal decNewRate = (decCurrentRate * decCurrentConversionRate) / decNewConversionRate;
                                            dgvProduction.Rows[e.RowIndex].Cells["dgvtxtProductionRate"].Value = Math.Round(decNewRate, 2);
                                        }
                                    }
                                    TotalAmountCalculationProduction("", e.RowIndex);
                                }
                            }

                            if (e.ColumnIndex == dgvProduction.Columns["dgvcmbProductionBatch"].Index)
                            {
                                BatchBll BllBatch = new BatchBll();
                                decimal decBatchId = 0;
                                string strBarcode = string.Empty;
                                if (dgvProduction.CurrentRow.Cells["dgvtxtProductionProductId"].Value != null)
                                {
                                    if (dgvProduction.CurrentRow.Cells["dgvcmbProductionBatch"].Value != null)
                                    {
                                        if (dgvProduction.CurrentRow.Cells["dgvcmbProductionBatch"].Value.ToString() != string.Empty &&
                                            dgvProduction.CurrentRow.Cells["dgvcmbProductionBatch"].Value.ToString() != "0")
                                        {
                                            decBatchId = Convert.ToDecimal(dgvProduction.CurrentRow.Cells["dgvcmbProductionBatch"].Value);
                                            strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                                            dgvProduction.CurrentRow.Cells["dgvtxtProductionBarcode"].Value = strBarcode;
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("SJ:81" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Function to fill the product details in dataGridView from productCreationPopup
 /// </summary>
 /// <param name="productcreation"></param>
 /// <param name="decproductId"></param>
 /// <param name="decCurrentRowIndex"></param>
 public void productDetailsFillFromProductCreation(frmProductCreation productcreation, decimal decproductId, decimal decCurrentRowIndex)
 {
     try
     {
         decimal decCurrentConversionRate = 0;
         UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
         List<DataTable> list = new List<DataTable>();
         BatchBll BllBatch = new BatchBll();
         ProductInfo infoProductFill = new ProductInfo();
         ProductCreationBll BllProductCreation = new ProductCreationBll();
         ProductInfo infoProduct = new ProductInfo();
         int inI = dgvPurchaseReturn.CurrentRow.Index;
         if (inI == dgvPurchaseReturn.Rows.Count - 1)
         {
             dgvPurchaseReturn.Rows.Add();
         }
         if (decproductId != 0)
         {
             infoProduct = BllProductCreation.ProductView(decproductId);
             SerialNo();
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtproductCode"].Value = infoProduct.ProductCode;
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtproductId"].Value = decproductId.ToString();
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtproductName"].Value = infoProduct.ProductName;
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbRack"].Value = infoProduct.RackId;
             UnitComboFill(infoProduct.ProductId, dgvPurchaseReturn.CurrentRow.Index, dgvPurchaseReturn.CurrentRow.Cells["dgvcmbUnit"].ColumnIndex);
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtrate"].Value = Math.Round(infoProduct.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProduct.UnitId, infoProduct.ProductId));
             dgvPurchaseReturn.CurrentRow.Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvPurchaseReturn.Rows[inI].Cells["dgvtxtUnitConversionId"].Value.ToString()));
             BatchComboFill(decproductId, inI, dgvPurchaseReturn.Rows[inI].Cells["dgvcmbBatch"].ColumnIndex);
             dgvPurchaseReturn.Rows[inI].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
             RackComboFill(infoProduct.GodownId, inI, dgvPurchaseReturn.Rows[inI].Cells["dgvcmbRack"].ColumnIndex);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtqty"].Value = "0";
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtbarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvPurchaseReturn.Rows[inI].Cells["dgvcmbBatch"].Value.ToString()));
             list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
             decCurrentConversionRate = Convert.ToDecimal(dgvPurchaseReturn.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());
             AmountCalculation("dgvtxtqty", inI);
             dgvPurchaseReturn.Rows[inI].Cells["dgvtxtqty"].Selected = true;
             dgvPurchaseReturn.Rows[inI + 1].Selected = false;
         }
         dgvPurchaseReturn.Rows[inI].HeaderCell.Value = "X";
         dgvPurchaseReturn.Rows[inI].HeaderCell.Style.ForeColor = Color.Red;
     }
     catch (Exception ex)
     {
         MessageBox.Show("PR:24" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to call frmProductSearchPopup form to select and view Product created 
 /// </summary>
 /// <param name="frmProductSearchPopup"></param>
 /// <param name="decproductId"></param>
 /// <param name="decCurrentRowIndex"></param>
 public void CallFromProductSearchPopup(frmProductSearchPopup frmProductSearchPopup, decimal decproductId, decimal decCurrentRowIndex)
 {
     try
     {
         base.Show();
         this.frmProductSearchPopupObj = frmProductSearchPopup;
         List<DataTable> list = new List<DataTable>();
         UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
         ProductInfo infoProduct = new ProductInfo();
         BatchBll BllBatch = new BatchBll();
         infoProduct = new ProductCreationBll().ProductView(decproductId);
         dgvProduct.Rows.Add();
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtProductCode"].Value = infoProduct.ProductCode;
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtProductId"].Value = decproductId.ToString();
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
         UnitComboFill1(decproductId, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Index, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbUnit"].ColumnIndex);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
         GridGodownComboFill(decproductId, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Index, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbGodown"].ColumnIndex);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
         RackComboFill1(infoProduct.GodownId, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Index, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbRack"].ColumnIndex);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbRack"].Value = infoProduct.RackId;
         BatchComboFill(decproductId, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Index, dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbBatch"].ColumnIndex);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvcmbBatch"].Value.ToString()));
         list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtUnitConversionId"].Value = list[0].Rows[0]["unitconversionId"].ToString();
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
         decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtConversionRate"].Value.ToString());
         AmountCalculation("dgvtxtQty", dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Index);
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].Cells["dgvtxtRate"].Value = infoProduct.SalesRate.ToString();
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].HeaderCell.Value = "X";
         dgvProduct.Rows[Convert.ToInt32(decCurrentRowIndex.ToString())].HeaderCell.Style.ForeColor = Color.Red;
         TotalAmountCalculation();
         frmProductSearchPopupObj.Close();
         frmProductSearchPopupObj = null;
     }
     catch (Exception ex)
     {
         MessageBox.Show("DN12:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// On leave from each cell of dgvProductDetails
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProductDetails_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            string strBarcode = string.Empty;
            decimal decDiscountPercent = 0;
            decimal decDiscount = 0;
            decimal decGrossValue = 0;
            try
            {
                BatchBll BllBatch = new BatchBll();
                if (e.ColumnIndex > -1 && e.RowIndex > -1)
                {
                    if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value != null)
                    {
                        if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value.ToString() != string.Empty && dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value.ToString() != "0")
                        {
                            if (e.ColumnIndex == dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtQuantity"].ColumnIndex)
                            {
                                if (dgvProductDetails.RowCount > 1)
                                {
                                    try
                                    {
                                        MaterialReceiptBll bllMaterialReceiptDetails = new MaterialReceiptBll();
                                        decimal decMaterialReceiptMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                                        List<DataTable> ListObj = new List<DataTable>();
                                        ListObj = bllMaterialReceiptDetails.MaterialReceiptDetailsViewByMaterialReceiptMasterIdWithRemainingByNotInCurrPI
                                        (decMaterialReceiptMasterId, decPurchaseMasterId, decPurchaseInvoiceVoucherTypeId);
                                        if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtMaterialReceiptDetailsId"].Value != null)
                                        {
                                            if (decimal.Parse(dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtMaterialReceiptDetailsId"].Value.ToString()) > 0)
                                            {
                                                if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value != null)
                                                {
                                                    if (decimal.Parse(dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value.ToString()) > decimal.Parse(dtblMeterialReceiptQty.Rows[e.RowIndex]["qty"].ToString()))
                                                    {
                                                        dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value = dtblMeterialReceiptQty.Rows[e.RowIndex]["qty"].ToString();
                                                        if (decMeterialReceiptQty < decimal.Parse(dtblMeterialReceiptQty.Rows[e.RowIndex]["qty"].ToString()))
                                                        {
                                                            dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value = decMeterialReceiptQty;
                                                            decMeterialReceiptQty = 0;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                            else if (e.ColumnIndex == dgvProductDetails.Columns["dgvcmbBatch"].Index)
                            {
                                if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null && dgvProductDetails.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value.ToString() != string.Empty)
                                {
                                    decimal decBatchId = Convert.ToDecimal(dgvProductDetails.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value);
                                    strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                                    dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                                }
                            }
                            else if (e.ColumnIndex == dgvProductDetails.Columns["dgvtxtGrossValue"].Index)
                            {
                                dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtDiscountPercent"].Value = 0;
                                dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtDiscount"].Value = 0;
                            }
                            else if (e.ColumnIndex == dgvProductDetails.Columns["dgvtxtDiscountPercent"].Index)
                            {
                                if (dgvProductDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                                {
                                    if (dgvProductDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != string.Empty)
                                    {
                                        if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtGrossValue"].Value != null)
                                        {
                                            if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtGrossValue"].Value.ToString() != string.Empty)
                                            {
                                                decDiscountPercent = Convert.ToDecimal(dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtDiscountPercent"].Value.ToString());
                                                decGrossValue = Convert.ToDecimal(dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtGrossValue"].Value.ToString());
                                                if (decGrossValue > 0)
                                                {
                                                    decDiscount = decGrossValue * decDiscountPercent / 100;
                                                }
                                                dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtDiscount"].Value = Math.Round(decDiscount, PublicVariables._inNoOfDecimalPlaces);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (e.ColumnIndex == dgvProductDetails.Columns["dgvtxtDiscount"].Index)
                            {
                                if (dgvProductDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                                {
                                    if (dgvProductDetails.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() != string.Empty)
                                    {
                                        if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtGrossValue"].Value != null)
                                        {
                                            if (dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtGrossValue"].Value.ToString() != string.Empty)
                                            {
                                                decDiscount = Convert.ToDecimal(dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtDiscount"].Value.ToString());
                                                decGrossValue = Convert.ToDecimal(dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtGrossValue"].Value.ToString());
                                                if (decGrossValue > 0)
                                                {
                                                    decDiscountPercent = decDiscount * 100 / decGrossValue;
                                                }
                                                dgvProductDetails.Rows[e.RowIndex].Cells["dgvtxtDiscountPercent"].Value = Math.Round(decDiscountPercent, PublicVariables._inNoOfDecimalPlaces);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    SerialNo();
                }
                CheckInvalidEntries(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("PI85:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// On cellEndEdit of grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProduct_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            ProductInfo infoProduct = new ProductInfo();
            BatchBll BllBatch = new BatchBll();
            ProductCreationBll BllProductCreation = new ProductCreationBll();
            StockPostingBll BllStockPosting = new StockPostingBll();
            //StockPostingSP spStockPosting = new StockPostingSP();
            try
            {
                isDoAfterGridFill = false;
                isValueChange = false;
                if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtProductName")
                {
                    string strProductName = string.Empty;
                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value.ToString() != string.Empty)
                    {
                        strProductName = dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value.ToString();
                    }
                    infoProduct = BllProductCreation.ProductViewByName(strProductName);
                    if (infoProduct.ProductCode != null && infoProduct.ProductCode != string.Empty)
                    {
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = infoProduct.ProductCode;
                        decimal decproductId = infoProduct.ProductId;
                        dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = infoProduct.ProductId;
                        BatchComboFill(decproductId, e.RowIndex, Convert.ToInt32(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].ColumnIndex));
                        decimal decBatchId = BllStockPosting.BatchViewByProductId(decproductId);
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = decBatchId;
                        decBatchId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value);
                        string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                        ListObj = BllProductCreation.ProductDetailsCoreespondingToBarcode(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value.ToString());
                        if (ListObj[0].Rows.Count > 0)
                        {
                            foreach (DataRow RowDetails in ListObj[0].Rows)
                            {
                                isValueChange = false;
                                dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = RowDetails["productId"].ToString();
                                decimal decProductId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value);
                                BatchComboFill(decProductId, e.RowIndex, Convert.ToInt32(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].ColumnIndex));
                                dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = Convert.ToDecimal(RowDetails["batchId"].ToString());
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = RowDetails["productCode"].ToString();
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value = RowDetails["productName"].ToString();
                                dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value = Convert.ToDecimal(RowDetails["godownId"].ToString());
                                decGodownId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value);
                                RackComboFill(decGodownId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].ColumnIndex);

                                dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = Convert.ToDecimal(RowDetails["rackId"].ToString());
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(Convert.ToDecimal(RowDetails["purchaseRate"].ToString()), PublicVariables._inNoOfDecimalPlaces).ToString();
                                UnitComboFill(decProductId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].ColumnIndex);
                                dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(RowDetails["unitId"].ToString());

                                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                               List< DataTable> listUnitByProduct = new List<DataTable>();
                               listUnitByProduct = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString());
                               foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                                {
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                                    {
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                                    }
                                }
                                decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                                decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                CalculateTotalAmount();
                            }
                        }
                    }
                    else
                    {
                        isValueChange = false;
                        dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtAmount"].Value = string.Empty;
                        isValueChange = true;
                    }
                }
                if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtProductCode")
                {
                    string strPrdCode = string.Empty;
                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString() != string.Empty)
                    {
                        strPrdCode = dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString();
                    }
                    isDoAfterGridFill = false;
                    infoProduct = BllProductCreation.ProductViewByCode(strPrdCode);
                    if (infoProduct.ProductId != 0)
                    {
                        decimal decproductId = infoProduct.ProductId;
                        dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = infoProduct.ProductId;
                        BatchComboFill(decproductId, e.RowIndex, Convert.ToInt32(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].ColumnIndex));
                        decimal decBatchId = BllStockPosting.BatchViewByProductId(decproductId);
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = decBatchId;
                        decBatchId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value);
                        string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                        ListObj = BllProductCreation.ProductDetailsCoreespondingToBarcode(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value.ToString());
                        if (ListObj[0].Rows.Count > 0)
                        {
                            foreach (DataRow RowDetails in ListObj[0].Rows)
                            {
                                isValueChange = false;
                                dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = RowDetails["productId"].ToString();
                                decimal decProductId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value);
                                BatchComboFill(decProductId, e.RowIndex, Convert.ToInt32(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].ColumnIndex));
                                dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = Convert.ToDecimal(RowDetails["batchId"].ToString());
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = RowDetails["productCode"].ToString();
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value = RowDetails["productName"].ToString();
                                dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value = Convert.ToDecimal(RowDetails["godownId"].ToString());
                                decGodownId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value);
                                RackComboFill(decGodownId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].ColumnIndex);
                                dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = Convert.ToDecimal(RowDetails["rackId"].ToString());
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(Convert.ToDecimal(RowDetails["purchaseRate"].ToString()), PublicVariables._inNoOfDecimalPlaces).ToString();
                                UnitComboFill(decProductId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].ColumnIndex);
                                dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(RowDetails["unitId"].ToString());
                                UnitConvertionBll bllUnitByProduct = new UnitConvertionBll();
                               List< DataTable >listUnitByProduct = new List<DataTable>();
                               listUnitByProduct = bllUnitByProduct.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString());
                               foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                                {
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                                    {
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                                    }
                                }
                                decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                                decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                CalculateTotalAmount();
                            }
                        }
                    }
                    else
                    {
                        isValueChange = false;
                        dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtAmount"].Value = string.Empty;
                        isValueChange = true;
                    }
                }
                if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtBarcode")
                {
                    string strBCode = string.Empty;
                   // DataTable dtbl = new DataTable();
                    if (!dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].ReadOnly && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value.ToString() != string.Empty)
                    {
                        strBCode = dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value.ToString();
                    }
                    isDoAfterGridFill = false;
                    ListObj = BllProductCreation.ProductDetailsCoreespondingToBarcode(strBCode);
                    if (ListObj[0].Rows.Count > 0)
                    {
                        foreach (DataRow RowDetails in ListObj[0].Rows)
                        {
                            isValueChange = false;
                            dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = RowDetails["productId"].ToString();
                            decimal decProductId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value);
                            BatchComboFill(decProductId, e.RowIndex, Convert.ToInt32(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].ColumnIndex));
                            dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = Convert.ToDecimal(RowDetails["batchId"].ToString());
                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = RowDetails["productCode"].ToString();
                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value = RowDetails["productName"].ToString();
                            dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value = Convert.ToDecimal(RowDetails["godownId"].ToString());
                            decGodownId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value);
                            RackComboFill(decGodownId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].ColumnIndex);
                            dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = Convert.ToDecimal(RowDetails["rackId"].ToString());
                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(Convert.ToDecimal(RowDetails["purchaseRate"].ToString()), PublicVariables._inNoOfDecimalPlaces).ToString();
                            UnitComboFill(decProductId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].ColumnIndex);
                            dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(RowDetails["unitId"].ToString());
                            UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                            List<DataTable> listUnitByProduct = new List<DataTable>();
                            listUnitByProduct = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString());
                            foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                            {
                                if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                                {
                                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                                }
                            }
                            decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                            decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                            NewAmountCalculation("dgvtxtQty", e.RowIndex);
                            CalculateTotalAmount();
                        }
                    }
                    else
                    {
                        isValueChange = false;
                        dgvProduct.Rows[e.RowIndex].Cells["productId"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = string.Empty;
                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtAmount"].Value = string.Empty;
                        isValueChange = true;
                    }
                }
                CheckInvalidEntries(e);
                isDoAfterGridFill = true;
                isValueChange = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("MR68:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// On cellLeave of dgvProduct
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProduct_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            BatchBll BllBatch = new BatchBll();
            PurchaseOrderBll BllPurchaseOrder = new PurchaseOrderBll();

            try
            {
                if (isValueChange)
                {
                    string strBarcode = string.Empty;
                    string strProductCode = string.Empty;
                    if (e.RowIndex > -1 && e.ColumnIndex > -1)
                    {
                        CheckInvalidEntries(e);
                        if (e.ColumnIndex == dgvProduct.Columns["dgvcmbBatch"].Index)
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["productId"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString() != string.Empty)
                            {
                                if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value.ToString() != string.Empty)
                                {
                                    if (Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value) != string.Empty &&
                                       Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value) != "0")
                                    {
                                        decBatchId = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value);
                                        strProductCode = dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString();
                                        strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value = strProductCode;
                                    }
                                }
                            }
                        }
                        else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() != string.Empty)
                            {
                                UnitConvertionBll bllUnitByProduct = new UnitConvertionBll();
                                List<DataTable> listUnitByProduct = new List<DataTable>();
                                listUnitByProduct = bllUnitByProduct.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["productId"].Value.ToString());
                                foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                                {
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                                    {
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                                        if (isDoAfterGridFill)
                                        {
                                            decimal decNewConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                            decimal decNewRate = (decCurrentRate * decCurrentConversionRate) / decNewConversionRate;
                                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(decNewRate, 2);
                                            NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                            CalculateTotalAmount();
                                        }
                                    }
                                }
                                CheckInvalidEntries(e);
                            }
                        }
                        else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbGodown")
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value.ToString() != string.Empty)
                            {
                                decGodownId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value);
                                RackComboFill(decGodownId, e.RowIndex, dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].ColumnIndex);
                                List<DataTable> listObj = new List<DataTable>();
                                RackBll BllRack = new RackBll();
                                listObj = BllRack.RackNamesCorrespondingToGodownId(decGodownId);
                                dgvProduct.Rows[e.RowIndex].Cells["dgvCmbRack"].Value = Convert.ToDecimal(listObj[0].Rows[0]["rackId"].ToString());
                            }
                            CheckInvalidEntries(e);
                        }
                        else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvCmbCurrency" && isAmountcalc)
                        {
                            ExchangeRateBll BllExchangeRate = new ExchangeRateBll();
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value != null && Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value) != string.Empty)
                            {
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtExchangeRate"].Value = BllExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value)));
                            }
                            else
                            {
                                dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value = BllPurchaseOrder.ExchangeRateIdByCurrencyId(PublicVariables._decCurrencyId);
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtExchangeRate"].Value = BllExchangeRate.GetExchangeRateByExchangeRateId(Convert.ToDecimal(Convert.ToString(dgvProduct.Rows[e.RowIndex].Cells["dgvCmbCurrency"].Value)));
                            }
                            CheckInvalidEntries(e);
                        }
                        //----------while changing Qty,corresponding change in amount--------
                        else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtQty" && isAmountcalc)
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value != null)
                            {
                                if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductCode"].Value.ToString() != string.Empty && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductName"].Value.ToString() != string.Empty)
                                {
                                    NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                    CalculateTotalAmount();
                                }
                            }
                            CheckInvalidEntries(e);
                        }
                        //---------------while changing Qty,corresponding change in amount----
                        else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtRate" && isAmountcalc)
                        {
                            NewAmountCalculation("dgvtxtRate", e.RowIndex);
                            CalculateTotalAmount();
                            CheckInvalidEntries(e);
                        }
                        //----while changing amount ,corresponding chnage in total amount-------
                        else if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtAmount" && isAmountcalc)
                        {
                            if (cmbcurrency.Text != string.Empty)
                            {
                                CalculateTotalAmount();
                            }
                        }
                        //}
                    }
                }
                CheckInvalidEntries(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("MR72:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        ///  Function to fill the product details in dataGridView from productCreationPopup
        /// </summary>
        /// <param name="productcreation"></param>
        /// <param name="decproductId"></param>
        /// <param name="decCurrentRowIndex"></param>
        public void productDetailsFillFromProductCreation(frmProductCreation productcreation, decimal decproductId, decimal decCurrentRowIndex)
        {
            try
            {
                decimal decCurrentConversionRate = 0;
                DataTable dtbl = new DataTable();
                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                BatchBll BllBatch = new BatchBll();
                ProductInfo infoProductFill = new ProductInfo();
                ProductCreationBll BllProductCreation = new ProductCreationBll();
                ProductInfo infoProduct = new ProductInfo();
                int inI = dgvProduct.CurrentRow.Index;
                if (inI == dgvProduct.Rows.Count - 1)
                {
                    dgvProduct.Rows.Add();
                }
                  if (decproductId != 0)
                    {
                        infoProduct = BllProductCreation.ProductView(decproductId);
                        SerialNo();
                        dgvProduct.Rows[inI].Cells["dgvtxtproductCode"].Value = infoProduct.ProductCode;
                        dgvProduct.Rows[inI].Cells["productId"].Value = decproductId.ToString();
                        dgvProduct.Rows[inI].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
                        dgvProduct.Rows[inI].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
                        dgvProduct.Rows[inI].Cells["dgvCmbRack"].Value = infoProduct.RackId;
                        UnitComboFill(decproductId, inI, dgvProduct.Rows[inI].Cells["dgvcmbUnit"].ColumnIndex);
                        dgvProduct.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;

                        List<DataTable> list = new  List<DataTable>();
                        list = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[inI].Cells["productId"].Value.ToString());
                        //UnitComboFill(infoProduct.ProductId, dgvProduct.CurrentRow.Index, dgvProduct.CurrentRow.Cells["dgvcmbUnit"].ColumnIndex);
                        //dgvProduct.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
                        dgvProduct.Rows[inI].Cells["dgvtxtRate"].Value = Math.Round(infoProduct.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
                        dgvProduct.Rows[inI].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProduct.UnitId, infoProduct.ProductId));
                        dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvtxtUnitConversionId"].Value.ToString()));
                        BatchComboFill(decproductId, inI, dgvProduct.Rows[inI].Cells["dgvcmbBatch"].ColumnIndex);
                        dgvProduct.Rows[inI].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
                        RackComboFill(infoProduct.GodownId, inI, dgvProduct.Rows[inI].Cells["dgvCmbRack"].ColumnIndex);
                        dgvProduct.Rows[inI].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvcmbBatch"].Value.ToString()));
                        list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
                        dgvProduct.Rows[inI].Cells["dgvtxtConversionRate"].Value = dtbl.Rows[0]["conversionRate"].ToString();
                        decCurrentConversionRate = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());

                        NewAmountCalculation("dgvtxtQty", inI);
                        CalculateTotalAmount();
                    }

            }
            catch (Exception ex)
            {
                MessageBox.Show("MR6:new" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// To select the product from ProductSearchPopup
        /// </summary>
        /// <param name="frmProductSearchPopup"></param>
        /// <param name="decproductId"></param>
        /// <param name="decCurrentRowIndex"></param>
        public void CallFromProductSearchPopup(frmProductSearchPopup frmProductSearchPopup, decimal decproductId, decimal decCurrentRowIndex)
        {
            ProductInfo infoProduct = new ProductInfo();
            ProductCreationBll BllProductCreation = new ProductCreationBll();
            UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
            BatchBll BllBatch = new BatchBll();
            try
            {
                base.Show();
                this.frmProductSearchPopupObj = frmProductSearchPopup;
                infoProduct = BllProductCreation.ProductView(decproductId);
                int inRowcount = dgvProduct.Rows.Count;

                for (int i = 0; i < inRowcount; i++)
                {
                    if (i == inRowcount - 1)
                    {
                        dgvProduct.Rows.Add();
                    }
                    if (i == decCurrentRowIndex)
                    {
                        SerialNo();
                        dgvProduct.Rows[i].Cells["dgvtxtproductCode"].Value = infoProduct.ProductCode;
                        dgvProduct.Rows[i].Cells["productId"].Value = decproductId.ToString();
                        dgvProduct.Rows[i].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
                        dgvProduct.Rows[i].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
                        dgvProduct.Rows[i].Cells["dgvCmbRack"].Value = infoProduct.RackId;
                        UnitComboFill(decproductId, i, dgvProduct.Rows[i].Cells["dgvcmbUnit"].ColumnIndex);
                        dgvProduct.Rows[i].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;

                        List<DataTable> list = new List<DataTable>();
                        list = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[i].Cells["productId"].Value.ToString());

                        dgvProduct.Rows[i].Cells["dgvtxtRate"].Value = Math.Round(infoProduct.PurchaseRate, PublicVariables._inNoOfDecimalPlaces);
                        dgvProduct.Rows[i].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(new UnitConvertionBll().UnitconversionIdViewByUnitIdAndProductId(infoProduct.UnitId, infoProduct.ProductId));
                        dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value = bllUnitConvertion.UnitConversionRateByUnitConversionId(Convert.ToDecimal(dgvProduct.Rows[i].Cells["dgvtxtUnitConversionId"].Value.ToString()));
                        BatchComboFill(decproductId, i, dgvProduct.Rows[i].Cells["dgvcmbBatch"].ColumnIndex);
                        dgvProduct.Rows[i].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
                        RackComboFill(infoProduct.GodownId, i, dgvProduct.Rows[i].Cells["dgvCmbRack"].ColumnIndex);
                        dgvProduct.Rows[i].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[i].Cells["dgvcmbBatch"].Value.ToString()));
                        list= bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
                        dgvProduct.Rows[i].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
                        decCurrentConversionRate = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());
                        NewAmountCalculation("dgvtxtQty", i);
                        CalculateTotalAmount();

                    }
                }
                frmProductSearchPopupObj.Close();
                frmProductSearchPopupObj = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("MR4:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Fill Details Corresponding Quotation No
 /// </summary>
 public void FillQuotationDetails()
 {
     SerialNo();
     try
     {
         BatchBll BllBatch = new BatchBll();
         SalesOrderDetailsInfo infoSalesOrderDetails = new SalesOrderDetailsInfo();
         TransactionsGeneralFillBll TransactionGeneralFillObj = new TransactionsGeneralFillBll();
         decimal decOrderNo = 0;
         if (!isEditFill)
         {
             inMaxCount = 0;
             decOrderNo = Convert.ToDecimal(cmbQuotationNo.SelectedValue.ToString());
             dgvSalesOrder.Rows.Clear();
             List<DataTable> ListObj = new SalesQuotationBll().QuotationMasterViewByQuotationMasterId(Convert.ToDecimal(cmbQuotationNo.SelectedValue.ToString()));
             if (ListObj[0].Rows.Count > 0)
             {
                 cmbPricingLevel.SelectedValue = ListObj[0].Rows[0]["pricingLevelId"].ToString();
                 if (ListObj[0].Rows[0]["employeeId"].ToString() != "")
                 {
                     strSalesMan = ListObj[0].Rows[0]["employeeId"].ToString();
                     cmbSalesMan.SelectedValue = strSalesMan;
                     if (cmbSalesMan.SelectedValue == null)
                     {
                         SalesManComboFill();
                         cmbSalesMan.SelectedValue = ListObj[0].Rows[0]["employeeId"].ToString();
                     }
                 }
                 DGVCurrencyComboFill();
                 cmbCurrency.SelectedValue = ListObj[0].Rows[0]["exchangeRateId"].ToString();
                 List<DataTable> ListObj1 = new SalesQuotationBll().SalesQuotationDetailsViewByquotationMasterIdWithRemainingBySO(Convert.ToDecimal(cmbQuotationNo.SelectedValue.ToString()), decSalesOrderMasterId);
                 int inRowIndex = 0;
                 foreach (DataRow drowDetails in ListObj1[0].Rows)
                 {
                     dgvSalesOrder.Rows.Add();
                     isValueChange = false;
                     isDoAfterGridFill = false;
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtSalesOrderDetailsId"].Value = drowDetails.ItemArray[0].ToString();
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtSalesQuotationDetailsId"].Value = drowDetails.ItemArray[0].ToString();
                     String ProductId = drowDetails.ItemArray[2].ToString();
                     infoProduct = new ProductCreationBll().ProductView(Convert.ToDecimal(ProductId));
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtBarcode"].Value = drowDetails["barcode"].ToString();
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtProductId"].Value = drowDetails["productId"];
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtProductCode"].Value = infoProduct.ProductCode;
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtProductName"].Value = drowDetails["productName"].ToString();
                     AssignProductDefaultValues(dgvSalesOrder.Rows.Count - 2, drowDetails["productId"].ToString());
                     TransactionGeneralFillObj.UnitViewAllByProductId(dgvSalesOrder, infoProduct.ProductId.ToString(), dgvSalesOrder.Rows.Count - 2);
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtQty"].Value = drowDetails["qty"].ToString();
                     UnitComboFill(Convert.ToDecimal(ProductId), dgvSalesOrder.Rows.Count - 2, dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbUnit"].ColumnIndex);
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(drowDetails["unitId"].ToString());
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtRate"].Value = drowDetails["rate"].ToString();
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtAmount"].Value = drowDetails["amount"].ToString();
                     BatchComboFill(Convert.ToDecimal(drowDetails["productId"].ToString()), dgvSalesOrder.Rows.Count - 2, dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbBatch"].ColumnIndex);
                     if (Convert.ToDecimal(drowDetails["batchId"].ToString()) != 0)
                     {
                         dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbBatch"].Value = Convert.ToDecimal(drowDetails["batchId"].ToString());
                     }
                     else
                     {
                         StockPostingBll BllStockPosting = new StockPostingBll();
                         decimal decBatchId = BllStockPosting.BatchViewByProductId(Convert.ToDecimal(ProductId));
                         dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbBatch"].Value = decBatchId;
                     }
                     string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbBatch"].Value.ToString()));
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtBarcode"].Value = strBarcode.ToString();
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtProductCode"].ReadOnly = true;
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtProductName"].ReadOnly = true;
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtBarcode"].ReadOnly = true;
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["inRowIndex"].Value = drowDetails["extra1"].ToString();
                     UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                     List<DataTable> list = new List<DataTable>();
                     list = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtProductId"].Value.ToString());
                     foreach (DataRow drUnitByProduct in list[0].Rows)
                     {
                         if (dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                         {
                             dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                             dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                         }
                     }
                     decCurrentConversionRate = Convert.ToDecimal(dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtConversionRate"].Value.ToString());
                     decCurrentRate = Convert.ToDecimal(dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvtxtRate"].Value.ToString());
                     if (cmbQuotationNo.Visible == true)
                     {
                         dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].Cells["dgvcmbUnit"].ReadOnly = true;
                     }
                     dgvSalesOrder.Rows[dgvSalesOrder.Rows.Count - 2].HeaderCell.Value = "";
                     int intIndex = 0;
                     int.TryParse(drowDetails["extra1"].ToString(), out intIndex);
                     if (inMaxCount < intIndex)
                         inMaxCount = intIndex;
                     inRowIndex++;
                     isValueChange = true;
                 }
                 for (int i = inRowIndex; i < dgvSalesOrder.Rows.Count; ++i)
                     dgvSalesOrder["inRowIndex", i].Value = GetNextinRowIndex().ToString();
                 SerialNo();
                 TotalAmountCalculation();
                 isDoAfterGridFill = true;
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("SO38: " + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// doing the unit conversion in cell enter
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvSalesOrder_CellEnter(object sender, DataGridViewCellEventArgs e)
 {
     SalesOrderMasterInfo infoSalesOrderMaster = new SalesOrderMasterInfo();
     SalesOrderBll bllSalesOrder = new SalesOrderBll();
     // decimal decQty =0;
     try
     {
         if (dgvSalesOrder.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewComboBoxCell)
         {
             dgvSalesOrder.EditMode = DataGridViewEditMode.EditOnEnter;
         }
         else
         {
             dgvSalesOrder.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
         }
         if (e.RowIndex != -1 && e.ColumnIndex != -1)
         {
             if (dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value != null && dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
             {
                 if (dgvSalesOrder.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                 {
                     if (dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value != null && dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString() != string.Empty)
                     {
                         if (dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtRate"].Value != null && dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString() != string.Empty)
                         {
                             decCurrentConversionRate = Convert.ToDecimal(dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                             decCurrentRate = Convert.ToDecimal(dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                         }
                     }
                 }
             }
             if (dgvSalesOrder.CurrentRow.Cells["dgvtxtProductId"].Value != null)
             {
                 if (dgvSalesOrder.CurrentRow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
                 {
                     BatchBll BllBatch = new BatchBll();
                     decimal decBatchId = Convert.ToDecimal(dgvSalesOrder.CurrentRow.Cells["dgvcmbBatch"].Value);
                     string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                     dgvSalesOrder.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                 }
             }
         }
         CheckInvalidEntries(e);
     }
     catch (Exception ex)
     {
         MessageBox.Show("SO75" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to call frmProductSearchPopup form to select and view Product
 /// </summary>
 /// <param name="frmProductSearchPopup"></param>
 /// <param name="decproductId"></param>
 /// <param name="decCurrentRowIndex"></param>
 public void CallFromProductSearchPopup(frmProductSearchPopup frmProductSearchPopup, decimal decproductId, decimal decCurrentRowIndex)
 {
     UnitConvertionBll bllUnitConversion = new UnitConvertionBll();
     try
     {
         this.frmProductSearchPopupObj = frmProductSearchPopup;
         List<DataTable> list = new List<DataTable>();
         BatchBll BllBatch = new BatchBll();
         ProductCreationBll BllProductCreation = new ProductCreationBll();
         if (decproductId != 0)
         {
             infoProduct = BllProductCreation.ProductView(decproductId);
             dgvSalesOrder.CurrentRow.Cells["dgvtxtProductCode"].Value = infoProduct.ProductCode.ToString();
             dgvSalesOrder.CurrentRow.Cells["dgvtxtProductId"].Value = decproductId.ToString();
             dgvSalesOrder.CurrentRow.Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
             dgvSalesOrder.CurrentRow.Cells["dgvtxtRate"].Value = infoProduct.SalesRate.ToString();
             UnitComboFill(decproductId, dgvSalesOrder.CurrentRow.Index, dgvSalesOrder.CurrentRow.Cells["dgvcmbUnit"].ColumnIndex);
             dgvSalesOrder.CurrentRow.Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
             BatchComboFill(decproductId, dgvSalesOrder.CurrentRow.Index, dgvSalesOrder.CurrentRow.Cells["dgvcmbBatch"].ColumnIndex);
             dgvSalesOrder.CurrentRow.Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decproductId);
             dgvSalesOrder.CurrentRow.Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvSalesOrder.CurrentRow.Cells["dgvcmbBatch"].Value.ToString()));
             list = bllUnitConversion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
             dgvSalesOrder.CurrentRow.Cells["dgvtxtUnitConversionId"].Value = list[0].Rows[0]["unitconversionId"].ToString();
             dgvSalesOrder.CurrentRow.Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
             decCurrentConversionRate = Convert.ToDecimal(dgvSalesOrder.CurrentRow.Cells["dgvtxtConversionRate"].Value.ToString());
             decCurrentRate = Convert.ToDecimal(dgvSalesOrder.CurrentRow.Cells["dgvtxtRate"].Value.ToString());
             AmountCalculation("dgvtxtQty", dgvSalesOrder.CurrentRow.Index);
             TotalAmountCalculation();
         }
         frmProductSearchPopupObj.Close();
         frmProductSearchPopupObj = null;
         this.Enabled = true;
         this.BringToFront();
     }
     catch (Exception ex)
     {
         MessageBox.Show("S10:" + ex.Message, "Open Miracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 public void ProductDetailsFill(int inRowIndex, string strFillMode)
 {
     try
     {
         ProductInfo infoProduct = new ProductInfo();
         BatchBll BllBatch = new BatchBll();
         StockPostingBll BllStockPosting = new StockPostingBll();
         //StockPostingSP spStockPosting = new StockPostingSP();
         string strPrdCode = string.Empty;
         string strProductName = string.Empty;
         if (strFillMode == "ProductCode")
         {
             if (dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductCode"].Value != null && dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductCode"].Value as string != string.Empty)
             {
                 strPrdCode = dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductCode"].Value.ToString();
             }
             infoProduct = new ProductCreationBll().ProductViewByCode(strPrdCode);
         }
         if (strFillMode == "ProductName")
         {
             if (dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductName"].Value != null && dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductName"].Value.ToString() != string.Empty)
             {
                 strProductName = dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductName"].Value.ToString();
             }
             infoProduct = new ProductCreationBll().ProductViewByName(strProductName);
         }
         if (infoProduct.ProductId != 0)
         {
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductCode"].Value = infoProduct.ProductCode;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
             string strproductId = Convert.ToString(infoProduct.ProductId);
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductId"].Value = infoProduct.ProductId;
             UnitComboFill1(Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtProductId"].Value), inRowIndex, dgvProduct.Rows[inRowIndex].Cells["dgvcmbUnit"].ColumnIndex);
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
             BatchComboFill(Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtProductId"].Value), inRowIndex, dgvProduct.Rows[inRowIndex].Cells["dgvcmbBatch"].ColumnIndex);
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbBatch"].Value = BllStockPosting.BatchViewByProductId(Convert.ToDecimal(dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductId"].Value));
             string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[inRowIndex].Cells["dgvcmbBatch"].Value.ToString()));
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
             GridGodownComboFill(Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvtxtProductId"].Value), inRowIndex, dgvProduct.Rows[inRowIndex].Cells["dgvcmbGodown"].ColumnIndex);
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
             RackComboFill1(Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value), inRowIndex, dgvProduct.Rows[inRowIndex].Cells["dgvcmbRack"].ColumnIndex);
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbRack"].Value = infoProduct.RackId;
             UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
             List<DataTable> ListObj = new List<DataTable>();
             ListObj = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(strproductId);
             foreach (DataRow drUnitByProduct in ListObj[0].Rows)
             {
                 if (dgvProduct.Rows[inRowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                 {
                     dgvProduct.Rows[inRowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                     dgvProduct.Rows[inRowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                 }
             }
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtRate"].Value = Math.Round(infoProduct.SalesRate, PublicVariables._inNoOfDecimalPlaces);
             decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[inRowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
             decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[inRowIndex].Cells["dgvtxtRate"].Value.ToString());
             decimal decProductId = Convert.ToDecimal(dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductId"].Value.ToString());
             decimal decBatchId = Convert.ToDecimal(dgvProduct.Rows[inRowIndex].Cells["dgvcmbBatch"].Value.ToString());
             getProductRate(inRowIndex, decProductId, decBatchId);
         }
         else
         {
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductName"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtRate"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductCode"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtBarcode"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbGodown"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbRack"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbUnit"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvcmbBatch"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtProductId"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtUnitConversionId"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtConversionRate"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtRate"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtAmount"].Value = string.Empty;
             dgvProduct.Rows[inRowIndex].Cells["dgvtxtQty"].Value = string.Empty;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("DN47:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to fill the details against purchase order
 /// </summary>
 public void FillOrderDetails()
 {
     BatchBll BllBatch = new BatchBll();
     PurchaseOrderBll BllPurchaseOrder = new PurchaseOrderBll();
     //StockPostingSP spStockPosting = new StockPostingSP();
     StockPostingBll BllStockPosting = new StockPostingBll();
     ProductCreationBll BllProductCreation = new ProductCreationBll();
     try
     {
         if (!isEditFill)
         {
             isValueChange = false;
             if (dgvProduct.RowCount > 1)
             {
                 for (int i = 0; i < dgvProduct.RowCount - 1; i++)
                 {
                     if (dgvProduct.Rows[i].Cells["dgvtxtMaterialReceiptdetailsId"].Value != null && dgvProduct.Rows[i].Cells["dgvtxtMaterialReceiptdetailsId"].Value.ToString() != string.Empty)
                     {
                         lstArrOfRemove.Add(dgvProduct.Rows[i].Cells["dgvtxtMaterialReceiptdetailsId"].Value.ToString());
                     }
                 }
             }
             dgvProduct.Rows.Clear();
             isValueChange = true;
             isDoAfterGridFill = false;
             List<DataTable> ListObjDetails = new List<DataTable>();
             if (Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString()) == decOrderNoWhileEditMode && btnSave.Text == "Update")
             {
                 ListObjDetails = BllPurchaseOrder.PurchaseOrderDetailsViewByOrderMasterIdWithRemainingForEdit(Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString()), decMaterialReceiptId);
             }
             else
             {
                 ListObjDetails = BllPurchaseOrder.PurchaseOrderDetailsViewByOrderMasterIdWithRemaining(Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString()), decMaterialReceiptId);
             }
             int inRowIndex = 0;
             foreach (DataRow drowDetails in ListObjDetails[0].Rows)
             {
                 dgvProduct.Rows.Add();
                 isValueChange = false;
                 isDoAfterGridFill = false;
                 dgvProduct.CurrentCell = dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtProductName"];
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtPurchaseOrderDetailsId"].Value = Convert.ToString(drowDetails.ItemArray[0]);
                 strproductId = drowDetails.ItemArray[2].ToString();
                 ProductInfo infoproduct = new ProductInfo();
                 infoproduct = BllProductCreation.ProductView(Convert.ToDecimal(strproductId));
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["productId"].Value = Convert.ToDecimal(strproductId);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtProductCode"].Value = infoproduct.ProductCode;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtProductName"].Value = infoproduct.ProductName;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtvouchertypeId"].Value = Convert.ToString(drowDetails.ItemArray[11]);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtvoucherNo"].Value = Convert.ToString(drowDetails.ItemArray[12]);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtinvoiceNo"].Value = Convert.ToString(drowDetails.ItemArray[13]);
                 UnitComboFill(Convert.ToDecimal(strproductId), dgvProduct.Rows.Count - 2, dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbUnit"].ColumnIndex);
                 isValueChange = true;
                 isDoAfterGridFill = true;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbUnit"].Value = Convert.ToDecimal(drowDetails["unitId"].ToString());
                 UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                List< DataTable >listUnitByProduct = new List<DataTable>();
                listUnitByProduct = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(strproductId);
                foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                 {
                     if (dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                     {
                         dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                         dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                     }
                 }
                 isValueChange = false;
                 isDoAfterGridFill = false;
                 BatchComboFill(Convert.ToDecimal(strproductId), dgvProduct.Rows.Count - 2, Convert.ToInt32(dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbBatch"].ColumnIndex));
                 decimal decBatch = BllStockPosting.BatchViewByProductId(Convert.ToDecimal(strproductId));
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbBatch"].Value = decBatch;
                 string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatch);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtBarcode"].Value = strBarcode;
                 DGVGodownComboFill();
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbGodown"].Value = Convert.ToDecimal(1);
                 RackComboFill(1, dgvProduct.Rows.Count - 2, dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvCmbRack"].ColumnIndex);
                 List<DataTable> listObj = new List<DataTable>();
                 RackBll BllRack = new RackBll();
                 listObj = BllRack.RackNamesCorrespondingToGodownId(1);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvCmbRack"].Value = Convert.ToDecimal(listObj[0].Rows[0]["rackId"].ToString());
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtQty"].Value = Convert.ToString(drowDetails.ItemArray[3]);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtRate"].Value = Convert.ToString(drowDetails.ItemArray[4]);
                 decCurrentRate = Convert.ToDecimal(drowDetails.ItemArray[4].ToString());
                 decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtConversionRate"].Value.ToString());
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtAmount"].Value = Convert.ToString(drowDetails.ItemArray[6]);
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtProductCode"].ReadOnly = true;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtProductName"].ReadOnly = true;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtBarcode"].ReadOnly = true;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvtxtAmount"].ReadOnly = true;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["inRowIndex"].Value = Convert.ToString(drowDetails["extra1"]);
                 if (cmbVoucherType.Text != "NA")
                 {
                     dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbUnit"].ReadOnly = true;
                 }
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].HeaderCell.Value = string.Empty;
                 int intIndex = 0;
                 int.TryParse(Convert.ToString(drowDetails["extra1"]), out intIndex);
                 if (inMaxCount < intIndex)
                     inMaxCount = intIndex;
                 inRowIndex++;
                 NewAmountCalculation(string.Empty, dgvProduct.Rows.Count - 2);
                 isValueChange = true;
                 isDoAfterGridFill = true;
                 dgvProduct.Rows[dgvProduct.Rows.Count - 2].Cells["dgvcmbUnit"].ReadOnly = true;
             }
             for (int i = inRowIndex; i < dgvProduct.Rows.Count; ++i)
                 dgvProduct["inRowIndex", i].Value = Convert.ToString(GetNextinRowIndex());
             SerialNo();
             CalculateTotalAmount();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("MR39:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 ///Function for ReturnFromProductCreationPopup
 /// </summary>
 public void ReturnFromProductCreation(decimal decProductId)
 {
     ProductInfo infoProduct = new ProductInfo();
     ProductCreationBll BllProductCreation = new ProductCreationBll();
     BatchBll BllBatch = new BatchBll();
     List<DataTable> list = new List<DataTable>();
     UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
     try
     {
         this.Enabled = true;
         this.BringToFront();
         int inI = dgvProduct.CurrentRow.Index;
         if (inI == dgvProduct.Rows.Count - 1)
         {
             dgvProduct.Rows.Add();
         }
         if (decProductId != 0)
         {
             infoProduct = BllProductCreation.ProductView(decProductId);
             dgvProduct.Rows[inI].Cells["dgvtxtProductCode"].Value = infoProduct.ProductCode.ToString();
             dgvProduct.Rows[inI].Cells["dgvtxtProductId"].Value = decProductId.ToString();
             dgvProduct.Rows[inI].Cells["dgvtxtProductName"].Value = infoProduct.ProductName;
             dgvProduct.Rows[inI].Cells["dgvtxtRate"].Value = infoProduct.SalesRate.ToString();
             UnitComboFill1(decProductId, inI, dgvProduct.Rows[inI].Cells["dgvcmbUnit"].ColumnIndex);
             dgvProduct.Rows[inI].Cells["dgvcmbUnit"].Value = infoProduct.UnitId;
             BatchComboFill(decProductId, inI, dgvProduct.Rows[inI].Cells["dgvcmbBatch"].ColumnIndex);
             dgvProduct.Rows[inI].Cells["dgvcmbBatch"].Value = BllBatch.BatchIdViewByProductId(decProductId);
             dgvProduct.Rows[inI].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvcmbBatch"].Value.ToString()));
             GridGodownComboFill(decProductId, dgvProduct.CurrentRow.Index, dgvProduct.Rows[inI].Cells["dgvcmbGodown"].ColumnIndex);
             dgvProduct.Rows[inI].Cells["dgvcmbGodown"].Value = infoProduct.GodownId;
             RackComboFill1(infoProduct.GodownId, inI, dgvProduct.Rows[inI].Cells["dgvcmbRack"].ColumnIndex);
             dgvProduct.Rows[inI].Cells["dgvcmbRack"].Value = infoProduct.RackId;
             list = bllUnitConvertion.DGVUnitConvertionRateByUnitId(infoProduct.UnitId, infoProduct.ProductName);
             dgvProduct.Rows[inI].Cells["dgvtxtUnitConversionId"].Value = list[0].Rows[0]["unitconversionId"].ToString();
             dgvProduct.Rows[inI].Cells["dgvtxtConversionRate"].Value = list[0].Rows[0]["conversionRate"].ToString();
             decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvtxtConversionRate"].Value.ToString());
             decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[inI].Cells["dgvtxtRate"].Value.ToString());
             AmountCalculation("dgvtxtQty", dgvProduct.CurrentRow.Index);
             dgvProduct.Rows[inI].HeaderCell.Value = "X";
             dgvProduct.Rows[inI].HeaderCell.Style.ForeColor = Color.Red;
             TotalAmountCalculation();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("DN25:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Doing the basic calculations in grid cell value change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProduct_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (isDoCellValueChange)
                {
                    decimal decOldQty = 0;
                    decimal decCurQty = 0;
                    decimal decNewAmount = 0;
                    if (e.RowIndex != -1 && e.ColumnIndex != -1)
                    {
                        if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbGodown")
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value != null)
                            {

                                DGVRackComboFill(Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value.ToString()), dgvProduct, e.RowIndex);
                            }
                        }
                        if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbBatch")
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null)
                            {
                                ProductCreationBll BllProductCreation = new ProductCreationBll();
                                decimal decRate = BllProductCreation.SalesInvoiceProductRateForSales(Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value), dtpDate.Value, Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value), Convert.ToDecimal(cmbPricingLevel.SelectedValue), PublicVariables._inNoOfDecimalPlaces);
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(decRate / Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value), PublicVariables._inNoOfDecimalPlaces);
                                BatchBll BllBatch = new BatchBll();
                                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value));
                            }
                        }
                        //------------Hided when asked Rejection In not to need unit conversion process -------------------

                        //if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                        //{
                        //    if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value.ToString() != string.Empty)
                        //    {
                        //        if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString() != string.Empty)
                        //        {
                        //            if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString() != string.Empty)
                        //            {
                        //                decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                        //                decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                        //            }
                        //        }
                        //    }

                        //}

                        if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvtxtQuantity")
                        {
                            DeliveryNoteDetailsInfo infodeliverynotedetails = new DeliveryNoteDetailsInfo();
                            //DeliveryNoteDetailsSP spdeliverynotedetails = new DeliveryNoteDetailsSP();
                            DeliveryNoteBll bllDeliveryNote = new DeliveryNoteBll();
                            List<DataTable> listObj = bllDeliveryNote.DeliveryNoteDetailsViewByDeliveryNoteMasterIdWithPending(Convert.ToDecimal(cmbDeliveryNoteNo.SelectedValue.ToString()), decRejectionInIdToEdit);
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value != null)
                            {
                                List<DataTable> listUnitByProduct = new List<DataTable>();
                                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                                listUnitByProduct = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value.ToString());
                                foreach (DataRow drdtbl in listObj[0].Rows)
                                {
                                    foreach (DataRow drUnitviewall in listUnitByProduct[0].Rows)
                                    {

                                        if (drdtbl["unitConversionId"].ToString() == drUnitviewall["unitConversionId"].ToString())
                                        {
                                            decimal decCurrentQty = Convert.ToDecimal(drdtbl.ItemArray[3].ToString());
                                            decimal decConRateToDlryNteDtls = Convert.ToDecimal(drUnitviewall.ItemArray[3].ToString());
                                            decimal decCurConRateInGrid = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                            decOldQty = (decCurrentQty / decConRateToDlryNteDtls) * decCurConRateInGrid;
                                            decOldQty = Math.Round(decOldQty, PublicVariables._inNoOfDecimalPlaces);
                                            decCurQty = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value.ToString());

                                        }
                                    }
                                }
                                if (decCurQty > decOldQty)
                                {
                                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value = decOldQty;

                                }
                                else if (decCurQty <= decOldQty)
                                {
                                    decimal decrate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtAmount"].Value = Math.Round(decrate * decCurQty, PublicVariables._inNoOfDecimalPlaces);
                                    CalcTotalAmt();
                                }
                            }
                        }

                        if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                        {
                            if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value != null)
                            {
                                List<DataTable> listUnitByProduct = new List<DataTable>();
                                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                                listUnitByProduct = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value.ToString());
                                foreach (DataRow drUnitByProduct in listUnitByProduct[0].Rows)
                                {
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                                    {
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtUnitConversionId"].Value = Convert.ToDecimal(drUnitByProduct[2].ToString());
                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                                        if (isDoAfterGridFill)
                                        {
                                            decimal decNewConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                            decCurQty = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQuantity"].Value.ToString());
                                            decimal decNewRate = (decCurrentRate * decCurrentConversionRate) / decNewConversionRate;
                                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(decNewRate);
                                            decNewAmount = (decCurrentRate * decCurQty * decCurrentConversionRate) / decNewConversionRate;
                                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtAmount"].Value = Math.Round(decNewAmount);
                                            txtTotalAmount.Text = Math.Round(decNewRate, 2).ToString();
                                            CalcTotalAmt();
                                        }

                                    }
                                }

                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("RI:41 " + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Grid view Cell Enter for set editmode and set basics of unit conversion
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void dgvProduct_CellEnter(object sender, DataGridViewCellEventArgs e)
 {
     decimal decGodownId = 0;
     try
     {
         if (dgvProduct.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewComboBoxCell)
         {
             dgvProduct.EditMode = DataGridViewEditMode.EditOnEnter;
         }
         else
         {
             dgvProduct.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
         }
         if (e.RowIndex != -1 && e.ColumnIndex != -1)
         {
             if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
             {
                 if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                 {
                     if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString() != string.Empty)
                     {
                         if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString() != string.Empty)
                         {
                             decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                             decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                         }
                     }
                 }
             }
             if (dgvProduct.CurrentRow.Cells["dgvtxtProductId"].Value != null)
             {
                 if (dgvProduct.CurrentRow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
                 {
                     BatchBll BllBatch = new BatchBll();
                     decimal decBatchId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbBatch"].Value);
                     string strBarcode = BllBatch.ProductBatchBarcodeViewByBatchId(decBatchId);
                     dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = strBarcode;
                 }
             }
             if (e.ColumnIndex == dgvProduct.Columns["dgvcmbRack"].Index)
             {
                 if (dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value != null)
                 {
                     if (dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value.ToString() != string.Empty)
                     {
                         if (dgvProduct.CurrentRow.Cells["dgvcmbRack"].Value == null || dgvProduct.CurrentRow.Cells["dgvcmbRack"].Value.ToString() == string.Empty)
                         {
                             decGodownId = Convert.ToDecimal(dgvProduct.CurrentRow.Cells["dgvcmbGodown"].Value);
                             RackComboFill1(decGodownId, e.RowIndex, e.ColumnIndex);
                         }
                     }
                 }
             }
         }
         CheckInvalidEntries(e);
     }
     catch (Exception ex)
     {
         MessageBox.Show("DN49:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// enables the enter key navigation for Cell EndEdit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProduct_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //decimal decCurQty = 0;
            //decimal decNewAmount = 0;
            try
            {
                // CheckInvalidEntries(e);
                UnitConvertionBll bllUnitConvertion = new UnitConvertionBll();
                if (isValueChange)
                {
                    if (isDoCellValueChange)
                    {
                        if (dgvProduct.Rows.Count > 0)
                        {
                            if (e.ColumnIndex == dgvProduct.Columns["dgvcmbRack"].Index)
                            {
                                if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbGodown")
                                {
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value != null)
                                    {
                                        RackComboFill(Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbGodown"].Value.ToString()), dgvProduct, e.RowIndex);
                                    }
                                }
                            }
                            if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbBatch")
                            {
                                if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value != null)
                                {
                                    BatchBll BllBatch = new BatchBll();
                                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtBarcode"].Value = BllBatch.ProductBatchBarcodeViewByBatchId(Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvcmbBatch"].Value));
                                }
                            }

                            if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                            {
                                if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value.ToString() != string.Empty)
                                {
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString() != string.Empty)
                                    {
                                        if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString() != string.Empty)
                                        {
                                            decCurrentConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtConversionRate"].Value.ToString());
                                            decCurrentRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value.ToString());
                                        }
                                    }
                                }

                            }
                            //---------------while changing Qty,corresponding change in amount----
                            if (dgvProduct.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtQty" && isAmountcalc)
                            {
                                if (cmbMaterialReceiptNo.SelectedValue == null || cmbMaterialReceiptNo.SelectedValue.ToString() == string.Empty)
                                {
                                    NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                }
                                else
                                {
                                    List<DataTable> Listobj= bllMaterialReceiptDetails.ShowMaterialReceiptDetailsViewbyMaterialReceiptDetailsIdWithPending(Convert.ToDecimal(cmbMaterialReceiptNo.SelectedValue.ToString()), decRejectionOutId);
                                    if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value != null)
                                    {
                                        List<DataTable> list = new List<DataTable>();

                                        list = bllUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value.ToString());
                                        foreach (DataRow drdtbl in Listobj[0].Rows)
                                        {
                                            foreach (DataRow drUnitviewall in list[0].Rows)
                                            {
                                                if (drdtbl.ItemArray[6].ToString() == drUnitviewall.ItemArray[2].ToString())//Checking UnitconversionId
                                                {
                                                    decimal decQty = Convert.ToDecimal(drdtbl.ItemArray[3].ToString());
                                                    decimal decConRate = Convert.ToDecimal(drUnitviewall.ItemArray[3].ToString());
                                                    decimal decCurConRateInGrid = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtconversionRate"].Value.ToString());
                                                    decOldQty = (decQty / decConRate) * decCurConRateInGrid;
                                                    decOldQty = Math.Round(decOldQty, PublicVariables._inNoOfDecimalPlaces);
                                                    decCurrQty = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value.ToString());
                                                    if (decCurrQty > decOldQty)
                                                    {
                                                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value = decOldQty.ToString();
                                                    }
                                                }
                                            }
                                        }
                                        //
                                        //decCurrQty = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value.ToString());
                                        //decOldQty = Convert.ToDecimal(dtblReceiptDetails.Rows[e.RowIndex].ItemArray[3].ToString());
                                        if (decCurrQty > decOldQty)
                                        {
                                            dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value = decOldQty.ToString();
                                        }
                                    }
                                    else
                                    {
                                        dgvProduct.Focus();
                                    }
                                    NewAmountCalculation("dgvtxtQty", e.RowIndex);
                                    TotalAmount();
                                }
                            }
                            //if (dgvProduct.Columns[e.ColumnIndex].Name == "dgvcmbUnit")
                            //{
                            //    if (dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value.ToString() != string.Empty)
                            //    {
                            //        if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value != null && dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() != string.Empty)
                            //        {
                            //            DataTable dtblUnitByProduct = new DataTable();
                            //            dtblUnitByProduct = spUnitConvertion.UnitConversionIdAndConRateViewallByProductId(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtproductId"].Value.ToString());
                            //            foreach (DataRow drUnitByProduct in dtblUnitByProduct.Rows)
                            //            {
                            //                if (dgvProduct.Rows[e.RowIndex].Cells["dgvcmbUnit"].Value.ToString() == drUnitByProduct.ItemArray[0].ToString())
                            //                {
                            //                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtunitConversionId"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[2].ToString());
                            //                    dgvProduct.Rows[e.RowIndex].Cells["dgvtxtconversionRate"].Value = Convert.ToDecimal(drUnitByProduct.ItemArray[3].ToString());
                            //                    if (isDoAfterGridFill)
                            //                    {
                            //                        decimal decNewConversionRate = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtconversionRate"].Value.ToString());
                            //                        decCurQty = Convert.ToDecimal(dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value.ToString());
                            //                        decimal decNewRate = (decCurrentRate * decCurrentConversionRate) / decNewConversionRate;
                            //                        //dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(decNewRate);
                            //                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtRate"].Value = Math.Round(decNewRate, 2);
                            //                        decNewAmount = (decCurrentRate * decCurQty * decCurrentConversionRate) / decNewConversionRate;
                            //                        dgvProduct.Rows[e.RowIndex].Cells["dgvtxtAmount"].Value = Math.Round(decNewAmount, 2);
                            //                        TotalAmount();
                            //                        txtTotalAmount.Text = Math.Round(decNewRate, 2).ToString();

                            //                    }
                            //                }
                            //            }
                            //            if (decCurrQty > decOldQty)
                            //            {
                            //                dgvProduct.Rows[e.RowIndex].Cells["dgvtxtQty"].Value = decOldQty.ToString();
                            //            }
                            //        }
                            //    }
                            //}

                            else if (dgvProduct.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtRate" && isAmountcalc)
                            {
                                NewAmountCalculation("dgvtxtRate", e.RowIndex);
                                TotalAmount();
                            }

                            else if (dgvProduct.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == "dgvtxtAmount" && isAmountcalc)
                            {
                                TotalAmount();
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("RO62:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }