我已经通过向 gridview、onRowDataBound 和 onDataBound 添加 2 个事件侦听器来完成第一部分。
这是我对这个方法的代码:
protected void GridViewOnRowDataBound(object sender, GridViewRowEventArgs e)
{
int i=0;
if (sender == GridView1)
i = 0;
else if (sender == GridView2)
i = 1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
SumaSpraw[i] += Convert.ToInt32(((Label)e.Row.FindControl("sprawy")).Text);
SumaAdresow[i] += Convert.ToInt32(((Label)e.Row.FindControl("adresy")).Text);
SumaInPost[i] += Convert.ToInt32(((Label)e.Row.FindControl("inpost")).Text);
SumaPoczta[i] += Convert.ToInt32(((Label)e.Row.FindControl("poczta")).Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((Label)e.Row.FindControl("sumaSpraw")).Text = SumaSpraw[i].ToString("N0");
((Label)e.Row.FindControl("sumaAdresow")).Text = SumaAdresow[i].ToString("N0");
((Label)e.Row.FindControl("sumaInPost")).Text = SumaInPost[i].ToString("N0");
((Label)e.Row.FindControl("sumaPoczta")).Text = SumaPoczta[i].ToString("N0");
}
}
protected void GridViewOnDataBound(object sender, EventArgs e)
{
int i = 0;
if (sender == GridView1)
i = 0;
else if (sender == GridView2)
i = 1;
decimal percent = 0M;
foreach (GridViewRow row in ((GridView)sender).Rows)
{
percent = (Convert.ToInt32(((Label)row.FindControl("adresy")).Text) / SumaAdresow[i]) * 100;
((Label)row.FindControl("procentAdresow")).Text = FormatPercent(Math.Round(percent, 2));
percent = (Convert.ToInt32(((Label)row.FindControl("inpost")).Text) / SumaInPost[i]) * 100;
((Label)row.FindControl("procentInPost")).Text = FormatPercent(Math.Round(percent, 2));
percent = (Convert.ToInt32(((Label)row.FindControl("poczta")).Text) / SumaPoczta[i]) * 100;
((Label)row.FindControl("procentPoczta")).Text = FormatPercent(Math.Round(percent, 2));
}
}
protected string FormatPercent(decimal percent)
{
if (percent == decimal.Truncate(percent))
{
return percent.ToString("N0") + "%";
}
else if (percent * 10 == decimal.Truncate(percent * 10))
{
return percent.ToString("N1") + "%";
}
else if (percent*100 == decimal.Truncate(percent*100))
{
return percent.ToString("N2") + "%";
}
else
return percent.ToString("N0") + "%";
}
第三种方法用于格式化输出。
但我仍然想知道第二部分 - 如何仅合并 2 行中的 2 个单元格(如在我的第二个屏幕中)。
我已经完成了造型部分。也许代码看起来并不那么棒,但它可以工作:)
public static void GridViewRowMerger(GridView gridView, int collIndex)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow currentRow = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
if (currentRow.Cells[collIndex].Text != previousRow.Cells[collIndex].Text) continue;
if (previousRow.Cells[collIndex].RowSpan < 2)
{
currentRow.Cells[collIndex].RowSpan = 2;
}
else
currentRow.Cells[collIndex].RowSpan = previousRow.Cells[collIndex].RowSpan + 1;
previousRow.Cells[collIndex].Visible = false;
}
}
public static void GridViewRefreshStyle(GridView gridView, int collIndex)
{
int rows = 0;
for (int rowIndex = 0; rowIndex < gridView.Rows.Count; rowIndex++)
{
if (gridView.Rows[rowIndex].Cells[collIndex].RowSpan > 1)
{
for (int i = 0; i < gridView.Rows[rowIndex].Cells[collIndex].RowSpan; i++)
{
gridView.Rows[rowIndex + i].CssClass = rows%2 == 0
? gridView.RowStyle.CssClass
: gridView.AlternatingRowStyle.CssClass;
}
rowIndex += gridView.Rows[rowIndex].Cells[collIndex].RowSpan - 1;
}
else
{
gridView.Rows[rowIndex].CssClass = rows%2 == 0
? gridView.RowStyle.CssClass
: gridView.AlternatingRowStyle.CssClass;
}
rows++;
}
}
因此,在绑定数据后执行 GridView 我只需调用这 2 个方法,如下所示:
GridView2.DataSource = dSet.Tables[1];
GridView2.DataBind();
GridViewRowMerger(GridView2, 0);
GridViewRefreshStyle(GridView2, 0);