private void btnSubmit_Click(object sender, EventArgs e) { Button oBtn = sender as Button; DataTable dt = new DataTable(); IList <TLCSV_PuchaseOrderDetail> PODetail = new List <TLCSV_PuchaseOrderDetail>(); IList <TLCSV_StockOnHand> SOHDetail = new List <TLCSV_StockOnHand>(); List <DATA> SOHGrouped = new List <DATA>(); BindingSource bindingSource1 = new BindingSource(); if (oBtn != null && formloaded) { if (UserD._External) { if (QueryParms.Customers.Count == 0) { MessageBox.Show("Please select a customer from the drop down list"); return; } if (QueryParms.Whses.Count == 0) { MessageBox.Show("Please select a warehouse fro the drop down list"); return; } } using (var context = new TTI2Entities()) { DGVOutput.DataSource = null; DGVOutput.Rows.Clear(); if (rbAvailable.Checked || rbGrossStock.Checked) { //============================================================ // Can be either Gross Stock or Available stock data needed // Start with Gross Stock always //------------------------------------------------------- SOHDetail = repo.GrossSOHQuery(QueryParms).ToList(); if (rbAvailable.Checked) { // Available Stock only //---------------------------------------------------------- SOHDetail = SOHDetail.Where(x => !x.TLSOH_Picked).ToList(); } if (SOHDetail.Count == 0) { DGVOutput.Visible = false; MessageBox.Show("No records found for selection made"); return; } var GroupedData = SOHDetail.GroupBy(x => new { x.TLSOH_Style_FK, x.TLSOH_Colour_FK, x.TLSOH_Size_FK }).ToList(); foreach (var Grouped in GroupedData) { var Sty = Grouped.FirstOrDefault().TLSOH_Style_FK; var Clr = Grouped.FirstOrDefault().TLSOH_Colour_FK; var xSize = Grouped.FirstOrDefault().TLSOH_Size_FK; var BoxQty = Grouped.Sum(x => (int?)x.TLSOH_BoxedQty ?? 0); var Record = SOHGrouped.Find(x => x._StyleFK == Sty && x._ColourFK == Clr && x._SizeFK == xSize); var index = SOHGrouped.IndexOf(Record); if (index < 0) { DATA dd = new DATA(); dd._BoxedQty = BoxQty; dd._ColourFK = Clr; dd._SizeFK = xSize; dd._StyleFK = Sty; SOHGrouped.Add(dd); } else { Record._BoxedQty += BoxQty; } } //now ready to display //------------------------------------------------- dt = new DataTable(); DataColumn column; // Create column 1 //---------------------------------------------- column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Style"; dt.Columns.Add(column); // Create column 2 //---------------------------------------------- column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Colour"; dt.Columns.Add(column); // Add the size names to the top of the grid box //------------------------------------------------------- foreach (var soh in SOHGrouped) { var xSize = context.TLADM_Sizes.Find(soh._SizeFK).SI_Description; if (!dt.Columns.Contains(xSize)) { dt.Columns.Add(xSize, typeof(Int32)); } else { continue; } } foreach (var soh in SOHGrouped) { var xStyle = context.TLADM_Styles.Find(soh._StyleFK).Sty_Description; var xColour = context.TLADM_Colours.Find(soh._ColourFK).Col_Display; var xSize = context.TLADM_Sizes.Find(soh._SizeFK).SI_Description; var index = dt.Columns.IndexOf(xSize); var SingleRow = (from Rows in dt.Rows.Cast <DataRow>() where Rows.Field <String>(0) == xStyle && Rows.Field <String>(1) == xColour select Rows).FirstOrDefault(); if (SingleRow != null) { SingleRow[index] = soh._BoxedQty; } else { var TheNewRow = dt.NewRow(); TheNewRow[0] = xStyle; TheNewRow[1] = xColour; TheNewRow[index] = soh._BoxedQty; dt.Rows.Add(TheNewRow); } } DataView DataV = dt.DefaultView; DataV.Sort = dt.Columns[0].ColumnName + "," + dt.Columns[1].ColumnName; DGVOutput.DataSource = DataV.ToTable(); DGVOutput.Visible = true; } else { //======================================================================= // this section of the code does the outstanding purchase orders //===================================================================== var Data = repo.POQuery(QueryParms); if (Data.Count() == 0) { DGVOutput.Visible = false; MessageBox.Show("No records found for selection made"); return; } var GroupedData = Data.GroupBy(x => new { x.TLCUSTO_Style_FK, x.TLCUSTO_Colour_FK, x.TLCUSTO_Size_FK }).ToList(); foreach (var Grouped in GroupedData) { var Sty = Grouped.FirstOrDefault().TLCUSTO_Style_FK; var Clr = Grouped.FirstOrDefault().TLCUSTO_Colour_FK; var xSize = Grouped.FirstOrDefault().TLCUSTO_Size_FK; var BoxQty = Grouped.Sum(x => (int?)x.TLCUSTO_Qty ?? 0); var Record = SOHGrouped.Find(x => x._StyleFK == Sty && x._ColourFK == Clr && x._SizeFK == xSize); var index = SOHGrouped.IndexOf(Record); if (index < 0) { DATA dd = new DATA(); dd._BoxedQty = BoxQty; dd._ColourFK = Clr; dd._SizeFK = xSize; dd._StyleFK = Sty; SOHGrouped.Add(dd); } else { Record._BoxedQty += BoxQty; } } //now ready to display //------------------------------------------------- dt = new DataTable(); DataColumn column; // Create column 1 //---------------------------------------------- column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Style"; dt.Columns.Add(column); // Create column 2 //---------------------------------------------- column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "Colour"; dt.Columns.Add(column); // Add the size names to the top of the grid box //------------------------------------------------------- foreach (var soh in SOHGrouped) { var xSize = context.TLADM_Sizes.Find(soh._SizeFK).SI_Description; if (!dt.Columns.Contains(xSize)) { dt.Columns.Add(xSize, typeof(Int32)); } else { continue; } } foreach (var soh in SOHGrouped) { var xStyle = context.TLADM_Styles.Find(soh._StyleFK).Sty_Description; var xColour = context.TLADM_Colours.Find(soh._ColourFK).Col_Display; var xSize = context.TLADM_Sizes.Find(soh._SizeFK).SI_Description; var index = dt.Columns.IndexOf(xSize); var SingleRow = (from Rows in dt.Rows.Cast <DataRow>() where Rows.Field <String>(0) == xStyle && Rows.Field <String>(1) == xColour select Rows).FirstOrDefault(); if (SingleRow != null) { SingleRow[index] = soh._BoxedQty; } else { var TheNewRow = dt.NewRow(); TheNewRow[0] = xStyle; TheNewRow[1] = xColour; TheNewRow[index] = soh._BoxedQty; dt.Rows.Add(TheNewRow); } } DataView DataV = dt.DefaultView; DataV.Sort = dt.Columns[0].ColumnName + "," + dt.Columns[1].ColumnName; DGVOutput.DataSource = DataV.ToTable(); DGVOutput.Visible = true; } } } }