private void RebindGrid() { // A function to return the value of difference amount from the group of orders found. Func <IEnumerable <DataRow>, decimal?> getImportedItemDifferenceAmount = drs => drs.Any(dr => dr.Field <int?>("PFInvoiceItemID").HasValue) ? drs.First(dr => dr.Field <int?>("PFInvoiceItemID").HasValue).Field <decimal?>("DifferenceAmount").Value : (decimal?)null; // Get Counts of Invoice Items Func <IEnumerable <DataRow>, int?> getImportedItemCount = drs => drs.Where(dr => dr.Field <int?>("PFInvoiceItemID").HasValue).Count(); // Get Invoice Totals //Func<IEnumerable<DataRow>, decimal?> getImportedItemValue = drs => drs.Where(dr => dr.Field<int?>("OrderID").HasValue).Sum(dr => dr.Field<decimal?>("Cost").Value); Func <IEnumerable <DataRow>, decimal?> getDifferenceItemValue = drs => drs.Where(dr => dr.Field <int?>("PFInvoiceItemID").HasValue).Sum(dr => dr.Field <decimal?>("DifferenceAmount").Value); Facade.IPalletForceImportPreInvoice facPFPreInvoice = new Facade.PreInvoice(); DataSet ds = null; switch (preInvoiceType) { case eInvoiceType.PFDepotCharge: ds = facPFPreInvoice.GetImportedDepotChargePreInvoice(PreInvoiceID); break; case eInvoiceType.PFHubCharge: ds = facPFPreInvoice.GetImportedHubChargePreInvoice(PreInvoiceID); break; case eInvoiceType.PFSelfBillDeliveryPayment: ds = facPFPreInvoice.GetImportedSelfBillDeliveryPreInvoice(PreInvoiceID); break; } if (ds != null) { int?unMatchedItems = 0; var queryInvoiceItems = ds.Tables[0].Rows.Cast <DataRow>().AsEnumerable(); var resultRows = from row in queryInvoiceItems group row by row["OrderID"] into g select new { OrderID = g.Key, DifferenceAmount = getImportedItemDifferenceAmount(g), OrderCount = g.Count(), Items = g }; lvPreInvoiceItems.DataSource = resultRows; lvPreInvoiceItems.DataBind(); // Get any unmatched palletforce invoice items. if (ds.Tables.Count > 1) { var queryUnmatchedItems = ds.Tables[1].Rows.Cast <DataRow>().AsEnumerable(); lvPreInvoiceUnMatchedItems.DataSource = queryUnmatchedItems; lvPreInvoiceUnMatchedItems.DataBind(); unMatchedItems = queryUnmatchedItems.Count(); } lblInvoiceItems.Text = getImportedItemCount(queryInvoiceItems).Value.ToString(); lblMatchedItems.Text = unMatchedItems.Value.ToString(); if (unMatchedItems.HasValue && unMatchedItems.Value > 0) { lblMatchedItems.ForeColor = System.Drawing.Color.Red; UnMatchedItems = unMatchedItems.Value; lblUnMatchedItems.Text = unMatchedItems.Value.ToString(); } else { lblMatchedItems.ForeColor = System.Drawing.Color.Black; lblUnMatchedItems.Text = "0"; } //lblSystemItemTotal.Text = getImportedItemValue(queryInvoiceItems).Value.ToString("C", ci); lblDifferenceValue.Text = getDifferenceItemValue(queryInvoiceItems).Value.ToString("C", ci); } }