/// <summary>
        /// THIS METHOD IS USED TO GET THE USER ENTERED DATA AND POPULATE THE RELATED BATCHES DATA SOURCES ON ITEM SELECTION CHANGED
        /// </summary>
        private void PopulateBatchTables()
        {
            if (dtDocuments.Rows.Count > 0)
            {
                var availableBatches = batchDataSources.Where(x => x.ItemId == currentItemCode && x.WhsCode == currentWhsCode);
                var selectedbatches = issueBatchDataSources.Where(x => x.ItemCode == currentItemCode && x.WhsCode == currentWhsCode && x.Index == currentDocumentIndex);

                if (availableBatches.Count() > 0)
                {
                    availableBatches.First().AvailableBatches = CopyBatchesDataTable(dtBatches);
                }
                else
                {
                    var bds = new BatchDataSources()
                    {
                        ItemId = currentItemCode,
                        WhsCode = currentWhsCode,
                        AvailableBatches = CopyBatchesDataTable(dtBatches)
                    };
                    batchDataSources.Add(bds);
                }
                if (selectedbatches.Count() > 0)
                {
                    selectedbatches.First().SelectedBatches = CopySelectedBatchesDataTable(dtSelected);
                }
                else
                {

                    double avgPrice = Convert.ToDouble(dtDocuments.GetValue("AvgPrice", currentDocumentIndex));
                    double addAmount = Convert.ToDouble(dtDocuments.GetValue("AddAmount", currentDocumentIndex));
                    double quantity = Convert.ToDouble(dtDocuments.GetValue("Quantity", currentDocumentIndex));
                    double totalNeeded = Convert.ToDouble(dtDocuments.GetValue("TotalNeeded", currentRecDocumentIndex));
                    var sbds = new SelectedBatchDataSources()
                    {
                        Quantity = Convert.ToDouble(quantity),
                        AddAmount = addAmount,
                        AvgPrice = Convert.ToDouble(avgPrice),
                        Index = currentDocumentIndex,
                        ItemCode = currentItemCode,
                        WhsCode = currentWhsCode,
                        SelectedBatches = CopySelectedBatchesDataTable(dtSelected),
                        TotalNeeded = totalNeeded
                    };
                    issueBatchDataSources.Add(sbds);
                }
            }
        }
        private void gvDocuments_ClickAfter(object sboObject, SAPbouiCOM.SBOItemEventArg pVal)
        {
            gvDocuments.Rows.IsSelected(pVal.Row);
            if (currentItemCode != string.Empty)
            {
                PopulateBatchTables();
            }

            if (pVal.Row >= 0)
            {
                string itemCode = dtDocuments.GetValue("ItemCode", pVal.Row).ToString();
                string whsCode = dtDocuments.GetValue("WhsCode", pVal.Row).ToString();
                double avgPrice = Convert.ToDouble(dtDocuments.GetValue("AvgPrice", pVal.Row));
                double addAmount = Convert.ToDouble(dtDocuments.GetValue("AddAmount", pVal.Row));
                double quantity = Convert.ToDouble(dtDocuments.GetValue("Quantity", pVal.Row));

                currentDocumentIndex = pVal.Row;
                currentItemCode = itemCode;
                currentWhsCode = whsCode;

                //CHECK IF THE CURRENT SELECTED ITEM HAS BATCHES TABLE POPULATED BEFORE
                var availableBatches = batchDataSources.Where(x => x.ItemId == currentItemCode && x.WhsCode == currentWhsCode);
                if (availableBatches.Count() > 0)
                {
                    dtBatches.Rows.Clear();
                    var tempBatches = availableBatches.First().AvailableBatches;

                    this.UIAPIRawForm.Freeze(true);
                    for (int x = 0; x < tempBatches.Rows.Count; x++)
                    {
                        dtBatches.Rows.Add(1);
                        dtBatches.SetValue("ItemCode", x, tempBatches.Rows[x]["ItemCode"]);
                        dtBatches.SetValue("DistNumber", x, tempBatches.Rows[x]["DistNumber"]);
                        dtBatches.SetValue("Quantity", x, tempBatches.Rows[x]["Quantity"]);
                        dtBatches.SetValue("WhsCode", x, tempBatches.Rows[x]["WhsCode"]);
                        dtBatches.SetValue("AvgPrice", x, tempBatches.Rows[x]["AvgPrice"]);
                        dtBatches.SetValue("Selected Qty", x, tempBatches.Rows[x]["Selected Qty"]);
                    }
                    this.UIAPIRawForm.Freeze(false);
                }
                else
                {
                    PopulateBatchesGrid(pVal.Row);
                    dtSelected.Rows.Clear();
                    System.Data.DataTable dt = CopyBatchesDataTable(dtBatches);
                    var bds = new BatchDataSources() { ItemId = currentItemCode, WhsCode = currentWhsCode, AvailableBatches = CopyBatchesDataTable(dtBatches) };
                    batchDataSources.Add(bds);
                }

                //CHECK IF THE SELECTED BATCHES TABLE HAS BEEN POPULATED BEFORE
                var selectedBatches = issueBatchDataSources.Where(x => x.ItemCode == currentItemCode && x.WhsCode == currentWhsCode && x.Index == currentDocumentIndex);
                if (selectedBatches.Count() > 0)
                {
                    dtSelected.Rows.Clear();
                    this.UIAPIRawForm.Freeze(true);
                    for (int x = 0; x < selectedBatches.First().SelectedBatches.Rows.Count; x++)
                    {
                        dtSelected.Rows.Add(1);
                        dtSelected.SetValue("Batch", x, selectedBatches.First().SelectedBatches.Rows[x]["Batch"]);
                        dtSelected.SetValue("Selected Qty", x, selectedBatches.First().SelectedBatches.Rows[x]["Selected Qty"]);

                    }
                    this.UIAPIRawForm.Freeze(false);
                }
                else
                {
                    dtSelected.Rows.Clear();
                    System.Data.DataTable dt = CopyBatchesDataTable(dtBatches);
                    var bds = new SelectedBatchDataSources()
                    {
                        AddAmount = addAmount,
                        AvgPrice = Convert.ToDouble(avgPrice),
                        Quantity = Convert.ToDouble(quantity),
                        Index = currentDocumentIndex,
                        ItemCode = currentItemCode,
                        WhsCode = currentWhsCode,
                        SelectedBatches = CopySelectedBatchesDataTable(dtSelected)
                    };
                    issueBatchDataSources.Add(bds);
                }
            }
        }