C# foreach循环、Linq循环之后增加汇总行

时间:2022-06-04 20:54:10

1.DataGridView增加汇总行(Linq循环获取数据)

private void BMbyBindData()
{
DateTime time1 = DateTime.Parse(DateTime.Now.ToShortDateString() + " 23:59:59");
DateTime time2 = DateTime.Parse(DateTime.Now.ToShortDateString() + " 00:00:00");

var Datainfo = (from name in new ReportTwo().BuMenWeek(time1, time2).AsEnumerable()
select new SumColumnModel
{
rows =(name[0]).ToString(),
BusinessUnitIdName = name[1].ToString(),
SystemUserIdName = name[2].ToString(),
zhoujifen = Convert.ToInt32(name[3]),
zong = Convert.ToInt32(name[4]),
}).ToList();

SumColumnModel zm = new SumColumnModel();//实例化类

zm.rows = ""; //增加汇总行的序号
zm.BusinessUnitIdName = "合计";
int a = Datainfo.Select(x => x.zong).Sum(); //得到各小组回执总和
double d = Convert.ToDouble(a) / 99999;
zm.SystemUserIdName = string.Format("{0:0.00%}", d); //得到完成率
zm.zhoujifen = Datainfo.Select(x => x.zhoujifen).Sum(); //周积分总和
zm.zong = Datainfo.Select(x => x.zong).Sum(); //各小组回执总和

List<SumColumnModel> zmlist = new List<SumColumnModel>();
Datainfo.Add(zm);

CR.DataSource = Datainfo;
CR.Height = CR.Rows.Count * CR.RowTemplate.Height + CR.ColumnHeadersHeight;

CR.ReadOnly = true;

CR.Columns["rows"].HeaderText = "排名";
CR.Columns["rows"].Width = 150;
CR.Columns["BusinessUnitIdName"].HeaderText = "组别";
CR.Columns["BusinessUnitIdName"].Width = 250;
CR.Columns["SystemUserIdName"].HeaderText = "完成率(回执总量/本组任务量)";
CR.Columns["SystemUserIdName"].Width = 300;
CR.Columns["zhoujifen"].HeaderText = "本周积分量";
CR.Columns["zhoujifen"].Width = 200;
CR.Columns["zong"].HeaderText = "总回执量";
CR.Columns["zong"].Width = CR.Width - 850;
}

2.foreach循环后增加汇总行

public static void ZuZhou()
{
StringBuilder messb = new StringBuilder();
if (mrzuznow != null)
{
messb.Append(@"<table width=""100%"">");
messb.Append("<tr>");
messb.Append(@"<td colspan=""4"">");
messb.Append("【本周小组排名】:");
messb.Append("</td>");
messb.Append("</tr>");
messb.Append("<tr>");
messb.Append("<td>");
messb.Append("排名 ");
messb.Append("</td>");
messb.Append("<td>");
messb.Append(" 组别");
messb.Append("</td>");
messb.Append("<td>");
messb.Append("回执总量 ");
messb.Append("</td>");
messb.Append("<td>");
messb.Append("完成率 ");
messb.Append("</td>");
messb.Append("<td>");
messb.Append("本周回执量 ");
messb.Append("</td>");
messb.Append("</tr>");

foreach (DataRow item in mrzuznow.Tables[0].Rows)
{
messb.Append("<tr>");
messb.Append("<td>");
messb.Append("TOP:【" + item["rows"].ToString() + "】");
messb.Append("</td>");
messb.Append(@"<td>" + item["BusinessUnitIdName"].ToString() + "");
messb.Append("</td>");
messb.Append(@"<td>" + item["zong"].ToString() + "");
double i = Math.Round(Convert.ToDouble(item["bv"]), 2);
string ii = (i * 100).ToString() + "%";
messb.Append("<td >" + ii + "");
messb.Append("</td>");
messb.Append(@"<td>" + item["zhoujifen"].ToString() + "");
messb.Append("</td>");
messb.Append("</tr>");

}

int rows = mrzuznow.Tables[0].Rows.Count + 1;

int sumzhoujifen = 0;
foreach (DataRow item in mrzuznow.Tables[0].Rows)
{
sumzhoujifen +=Convert.ToInt32( item["zhoujifen"]);
}
int Sumzong = 0;
foreach (DataRow item in mrzuznow.Tables[0].Rows)
{
Sumzong += Convert.ToInt32(item["zong"]);
}


double d = Convert.ToDouble(Sumzong) / 38291;
string SumSystemUserIdName = string.Format("{0:0.00%}", d); //得到完成率

messb.Append("<tr>");
messb.Append("<td>");
messb.Append("");
messb.Append("</td>");
messb.Append("<td>");
messb.Append(" 合计");
messb.Append("</td>");
messb.Append("<td>");
messb.Append("" + Sumzong + "");
messb.Append("</td>");
messb.Append("<td>");
messb.Append("" + SumSystemUserIdName + " ");
messb.Append("</td>");
messb.Append("<td>");
messb.Append(""+sumzhoujifen+"");
messb.Append("</td>");
messb.Append("</tr>");
messb.Append("</table>");
}
}