0

我正在页面上创建一个简单的网格视图。数据来自一个 sql 过程。这是它现在的样子:

在此处输入图像描述

我的第一个问题是如何计算“%”列的百分比。公式很简单:例如在选定的单元格中它应该是 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];
    }

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

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

在此处输入图像描述

4

1 回答 1

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 我只需调用这 2 个方法,如下所示:

GridView2.DataSource = dSet.Tables[1];
GridView2.DataBind();
GridViewRowMerger(GridView2, 0);
GridViewRefreshStyle(GridView2, 0);
于 2012-04-27T06:09:13.930 回答