日期:2014-05-20 浏览次数:21114 次
public static void 小计(DataGridView ucgrd, string col)
{
for (int i = 0; i < ucgrd.Columns.Count; i++)
{
ucgrd.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
//清除grd背景色
for (int ii = 0; ii < ucgrd.Rows.Count; ii++)
{
ucgrd.Rows[ii].DefaultCellStyle.BackColor = Color.White;
}
//总计
string[] str小计 = clsString.Split空格(col);
ucgrd.RowCount = ucgrd.RowCount + 1;
//在ucgrd.RowCount-1行插入总计行
for (int i = 0; i < str小计.Length - 1; i++)
{
int 列 = clsC.Cint(str小计[i]);
//遍历整个表,相加得总计和
for (int 行 = 0; 行 < ucgrd.RowCount - 1; 行++)
{
double 数量1 = 0, 数量2 = 0;
if (ucgrd.Rows[ucgrd.RowCount - 1].Cells[列].Value != null)
{
数量1 = clsC.Cdbl(ucgrd.Rows[ucgrd.RowCount - 1].Cells[列].Value.ToString());
}
if (ucgrd.Rows[行].Cells[列].Value != null)
{
数量2 = clsC.Cdbl(ucgrd.Rows[行].Cells[列].Value.ToString());
}
ucgrd.Rows[ucgrd.RowCount - 1].Cells[列].Value = 数量1 + 数量2;
}
}
ucgrd.Rows[ucgrd.RowCount - 1].Cells[0].Value = "";
ucgrd.Rows[ucgrd.RowCount - 1].Cells[1].Value = "总计";
//颜色
ucgrd.Rows[ucgrd.RowCount - 1].DefaultCellStyle.BackColor = Color.YellowGreen;
//小计
int 汇总位, J = 0;
汇总位 = 0;
while (1 == 1)
{
//如果到了列表的末尾,就不要小计了
if (J == ucgrd.RowCount - 1)
{
break;
}
//判断小计是否一类
if (ucgrd.Rows[J + 1].Cells[0].Value != null && ucgrd.Rows[J].Cells[0].Value != null)
{
//不相等就执行,否则跳过,直到同一类的完再执行小计
if (!ucgrd.Rows[J].Cells[0].Value.ToString().Equals(ucgrd.Rows[J + 1].Cells[0].Value.ToString()))
{
ucgrd.Rows.Insert(J + 1);
for (int i = 0; i < str小计.Length - 1; i++)
{
int 列 = clsC.Cint(str小计[i]);
for (int 行 = 汇总位; 行 < J + 1; 行++)
{
double 数量3 = 0, 数量4 = 0;
if (ucgrd.Rows[行].Cells[列].Value != null)
{
数量3 = clsC.Cdbl(ucgrd.Rows[行].Cells[列].Value.ToString());
}
if (ucgrd.Rows[J + 1].Cells[列].Value != null)
{
数量4 = clsC.Cdbl(ucgrd.Rows[J + 1].Cells[列].Value.ToString());
}
ucgrd.Rows[J + 1].Cells[列].Value = 数量3 + 数量4;
}
}
ucgrd.Rows[J + 1].Cells[0].Value = "";
ucgrd.Rows[J + 1].Cells[1].Value = "小计";
//颜色
//ucgrd.Rows[J + 1].DefaultCellStyle.BackColor = Color.Cyan;
ucgrd.Rows[J + 1].DefaultCellStyle.BackColor = Color.PaleGoldenrod;
汇总位 = J + 2;
J = J + 1; //跳一行小计的那一行就不比较了
}
}
J = J + 1;
}
}