显示gridview页脚

时间:2016-10-14 08:59:06

标签: c# asp.net gridview generic-list

需要关于表格页脚显示总和的帮助,我使用通用列表而不是数据表,当我谷歌解决方案时,我只能获得数据表的指南,但没有通用列表。

    protected void FillRequestGrid()
{
    if (Session["loggedUserID"] == null && Session["loggedRoleID"] == null)
    {
        Response.Redirect("Login.aspx");
    }
    else
    {
        int loggedUserID = Convert.ToInt32(Session["loggedUserID"]);
        List<BOL.UserInfo> userslist = new UserInfos().List();
        BOL.UserInfo loggeduser = userslist.Where(x => x.UserID == loggedUserID).FirstOrDefault();

        List<BOL.HomeAnnouncement> train_L = new Homes().ATTENDANCE(loggeduser.SUBSIDIARY_CD, "7.00 am - 7.00 pm");

        GrdUsers.DataSource = train_L;
        GrdUsers.DataBind();

    }
}

<asp:GridView ID="GrdUsers" runat="server" CssClass="table table-striped table-bordered table-hover" EmptyDataText="No Records Found" DataKeyNames="Description"
    AllowPaging="true" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" RowStyle-HorizontalAlign="Left" OnPageIndexChanging="GrdUsers_PageIndexChanging1"
    HeaderStyle-HorizontalAlign="Center" GridLines="None" BorderWidth="2px" EditRowStyle-BorderColor="#000000" EmptyDataRowStyle-BorderStyle="Groove" PageSize="10"
    Width="70%" RowStyle-CssClass="gradeX"  AlternatingRowStyle-CssClass="gradeA">
    <Columns>
        <asp:TemplateField HeaderText="Area" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="15%">
            <ItemTemplate>
                <label style="color: black; font-weight: 100"><%#DataBinder.Eval(Container.DataItem, "Description")%></label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total Employee" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="10%">
            <ItemTemplate>
                <label style="color: black; font-weight: bold"><%#DataBinder.Eval(Container.DataItem, "a_1")%></label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Present" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="7%">
            <ItemTemplate>
                <label style="color: black; font-weight: 100"><%#DataBinder.Eval(Container.DataItem, "a_2")%></label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Absent" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="7%">
            <ItemTemplate>
                <label style="color: black; font-weight: 100"><%#DataBinder.Eval(Container.DataItem, "status")%></label>
            </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Percent of employee present(%)" HeaderStyle-BackColor="#0033cc" HeaderStyle-ForeColor="White" ItemStyle-Width="15%">
            <ItemTemplate>
                <label style="color: black; font-weight: 100"><%#DataBinder.Eval(Container.DataItem, "PURPOSE")%></label>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
    <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
</asp:GridView>

期待这个输出:

enter image description here

我当前的输出数据没有页脚。

1 个答案:

答案 0 :(得分:0)

使用页脚模板。

<asp:TemplateField HeaderText="Total Employee">
<ItemTemplate>
<asp:Label ID="lblamount" runat="server" Text='<%# Eval("Total Employee") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalEmployee" runat="server" />
</FooterTemplate>
</asp:TemplateField>

并添加Gridview RowDataBound事件以计算所有员工的总和。

int total = 0;
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Total Employee"));
}
if(e.Row.RowType==DataControlRowType.Footer)
{
Label lblTotal= (Label)e.Row.FindControl("lblTotalEmployee");
lblTotal.Text = total.ToString();
}
}

参见参考资料

Ref 1

Ref 2

Ref 3