仅可见行的列总和

时间:2019-01-30 15:53:11

标签: c# asp.net gridview

我试图计算总计列的总和并将其显示给gridview的页脚,我能够实现这一点。但是,我有一个条件,使某些行从gridview中不可见,但同时也对不可见的行的值求和。如何只对可见行汇总列值?

下面是我的网格视图:

<asp:GridView ID="GridView9" runat="server" AllowPaging="false" AutoGenerateColumns="False"
            ShowFooter="true" BackColor="White" DataKeyNames="ID" BorderColor="#999999" CssClass="tableUserInfo"
            GridLines="Vertical" ShowHeaderWhenEmpty="True" PageSize="10" OnRowDataBound="GridView9_RowDataBound">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <Columns>
                <asp:TemplateField HeaderText="SessionId" Visible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txt_GSessionId" runat="server" Text='<%# Eval("SessionId") %>' ReadOnly="true"
                            Visible="false"></asp:TextBox>
                    </ItemTemplate>
                    <HeaderStyle Width="10%" />
                    <ItemStyle Width="10%" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Customer" Visible="true">
                    <ItemTemplate>
                        <asp:Label ID="lblCustomerName" Font-Size="11px" runat="server" Text='<%# Eval("CustomerName") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Width="8%" />
                    <ItemStyle Width="8%" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="G_Total" Visible="true">
                    <ItemTemplate>
                        <asp:Label ID="lblGTotal" Font-Size="11px" runat="server" Text='<%# Eval("GTotal","{0:n}") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotal" runat="server" Text="Label"></asp:Label>
                    </FooterTemplate>
                    <FooterStyle Width="30%" />
                    <HeaderStyle Width="8%" />
                    <ItemStyle Width="8%" />
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="Black" ForeColor="Red" Width="100%" Wrap="false" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        </asp:GridView>

这是我后面的代码:

decimal grdTotal;
protected void GridView9_RowDataBound(object sender, GridViewRowEventArgs e)
{
    txt_SessionId.Text = ddlSession.SelectedValue;
    foreach (GridViewRow row2 in GridView9.Rows)
    {
        TextBox sid = row2.FindControl("txt_GSessionId") as TextBox;
        int a, b;
        a = int.Parse(sid.Text);
        b = int.Parse(txt_SessionId.Text);
        if (a == b)
        {
            row2.Visible = true;
        }
        else
        {
            row2.Visible = false;
        }
    }

    if (e.Row.Visible && e.Row.RowType == DataControlRowType.DataRow)
    {
        decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "GTotal"));
        grdTotal = grdTotal + rowTotal;
    }

    if (e.Row.RowType == DataControlRowType.Footer)
    {
        Label lbl = (Label)e.Row.FindControl("lblTotal");
        Label lbl1 = (Label)e.Row.FindControl("lblLabourTotal");
        lbl.Font.Size = 12;
        lbl.Text = "Rs. " + grdTotal.ToString("0");
        lbl.ForeColor = System.Drawing.Color.White;
    }
}

0 个答案:

没有答案