Gridview - 摘要和百分比列

时间:2012-04-26 12:01:41

标签: asp.net sql-server-2005 gridview

我正在页面上创建一个简单的gridview。 数据来自sql过程。 以下是它现在的样子:

enter image description here

我的第一个问题是如何计算“%”列的百分比。公式非常简单: 例如,在选定的单元格中,它应为15612/238171 * 100%

我正在计算每一行的摘要:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="sprawy" runat="server" Text='<%#Sprawy(Eval("sprawy"),1) %>' /> 
    </ItemTemplate>
    <FooterTemplate>
        <asp:Label ID="sumaSpraw" runat="server" Text='<%#GetSumaSpraw(1) %>' /> 
    </FooterTemplate>
</asp:TemplateField> 

这是我在gridview中的列代码

这是函数的代码:

    public Int32[] SumaSpraw = new Int32[4];

    public Int32 Sprawy(object arg1, int i)
    {
        var ilosc = arg1 != DBNull.Value ? Convert.ToInt32(arg1) : 0;
        SumaSpraw[i - 1] += ilosc;
        return ilosc;
    }

    public Int32 GetSumaSpraw(int i)
    {
        return SumaSpraw[i - 1];
    }

我想知道如何根据一行和​​单元格值的汇总来计算“%”单元格的值。

我的第二个问题: 是否可以修改网格视图上的视图以显示组中的数据,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:0)

我设法通过向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我就像这样调用这两个方法:

GridView2.DataSource = dSet.Tables[1];
GridView2.DataBind();
GridViewRowMerger(GridView2, 0);
GridViewRefreshStyle(GridView2, 0);