private void loadMonthData(int year, int month) { List <Models.GoodsCategory> cats = this.cmbCategory.DataSource as List <Models.GoodsCategory>; if (this.dgvMonthDay.Columns.Count < 1) { //设置DataGridView列头 dgvMonthDay.Columns.Add(new DataGridViewTextBoxColumn()); dgvMonthDay.Columns[0].HeaderText = "日期"; foreach (Models.GoodsCategory cat in cats) { dgvMonthDay.Columns.Add(new DataGridViewTextBoxColumn()); dgvMonthDay.Columns[dgvMonthDay.Columns.Count - 1].HeaderText = cat.Name; } dgvMonthDay.Columns.Add(new DataGridViewTextBoxColumn()); dgvMonthDay.Columns[dgvMonthDay.Columns.Count - 1].HeaderText = "合计"; } //合计行 lastRow = new DataGridViewRow(); lastRow.Cells.Add(this.getDateGridViewCell("合计")); foreach (Models.GoodsCategory cat in cats) { lastRow.Cells.Add(this.getDateGridViewCell(0, cat.Id)); } Decimal total = 0; this.dgvMonthDay.Rows.Clear(); //填充数据 List <ReportGoodsRank> dataSources = bll.GroupStatisticsByDay(year, month); int endDay = Convert.ToDateTime(Convert.ToDateTime(String.Format("{0}-{1}-01", year, month)).AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1).Day; if (year == DateTime.Now.Year && month == DateTime.Now.Month) { endDay = DateTime.Now.Day; } for (int day = 1; day <= endDay; day++) { DataGridViewRow row = new DataGridViewRow(); //添加月份 row.Cells.Add(this.getDateGridViewCell(day.ToString() + "日")); decimal cellTotal = 0; foreach (GoodsCategory cat in cats) { Boolean flag = false; foreach (ReportGoodsRank item in dataSources) { if (item.Month == day && cat.Name.Trim() == item.GoodsName.Trim()) { flag = true; cellTotal += item.Price; row.Cells.Add(this.getDateGridViewCell(cat.Name.Trim() == "刷卡" ? item.Count : item.Price)); //统计某类商品年度销售总额 GetValue(cat, item); break; } } if (flag) { continue; } row.Cells.Add(this.getDateGridViewCell("0")); } total += cellTotal; //添加月份销售额小计 row.Cells.Add(this.getDateGridViewCell(cellTotal)); dgvMonthDay.Rows.Add(row); } lastRow.Cells.Add(this.getDateGridViewCell(total)); dgvMonthDay.Rows.Add(lastRow); }