如何将Footer添加到Gridview

时间:2021-12-11 07:46:39

How can i add a separate row to grid view to show the totals. My design looks like this 如何将Footer添加到Gridview

如何向网格视图添加单独的行以显示总计。我的设计看起来像这样

<asp:GridView ID="detailsGrid" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
                BorderColor="#2B80FF" BorderStyle="Solid" BorderWidth="1px" Width="100%" OnPageIndexChanging="detailsGrid_PageIndexChanging">
                <Columns>

                    <asp:BoundField HeaderText="Invoices Amount ($)" DataField="InvoiceAmount" />
                    <asp:BoundField DataField="ReceivedAmount" HeaderText="Received Amount($)" />
                    <asp:BoundField HeaderText="Balance Amount($)" DataField="BalanceAmount" />
                    <asp:BoundField HeaderText="Consumers Name" DataField="ConsumerEmailID" />
                </Columns>                    
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#b3b3b3" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                <PagerStyle BackColor="#B8C9EA" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

            </asp:GridView>

2 个解决方案

#1


1  

Use a footer template: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx

使用页脚模板:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx

The link contains a sample that shows how to add a total field. Basically:

该链接包含一个示例,显示如何添加总字段。基本上:

<asp:templatefield headertext="Total"
            itemstyle-horizontalalign="Right"
            footerstyle-horizontalalign="Right"
            footerstyle-backcolor="Blue"
            footerstyle-forecolor="White">
            <itemtemplate>
              <%#Eval("Total", "{0:c}") %>
            </itemtemplate>
            <footertemplate>
              <asp:label id="OrderTotalLabel"
                runat="server"/>
            </footertemplate>
          </asp:templatefield>

Along with:

随着:

void OrderGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

      // Get the cell that contains the item total.
      TableCell cell = e.Row.Cells[2];

      // Get the DataBoundLiteralControl control that contains the 
      // data-bound value.
      DataBoundLiteralControl boundControl = (DataBoundLiteralControl)cell.Controls[0];

      // Remove the '$' character so that the type converter works properly.
      String itemTotal = boundControl.Text.Replace("$",  "");

      // Add the total for an item (row) to the order total.
      orderTotal += Convert.ToDecimal(itemTotal);

    }

  }

#2


1  

Use Footer row to do that

使用页脚行来执行此操作

Check this link http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html

查看此链接http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html

in the RowDataBind event check for footer as follows

在RowDataBind事件中检查页脚如下

float _total;
protected void grdv_cart_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  DataRow dr = ((DataRowView)e.Row.DataItem).Row;
  float itemPrice = float.Parse(dr["ItemPrice"].ToString());
  _total += itemPrice;
 }
if (e.Row.RowType == DataControlRowType.Footer)
{
  //here write the totals into labels
}

if any doubts give me a comment!

如果有任何疑问给我评论!

#1


1  

Use a footer template: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx

使用页脚模板:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx

The link contains a sample that shows how to add a total field. Basically:

该链接包含一个示例,显示如何添加总字段。基本上:

<asp:templatefield headertext="Total"
            itemstyle-horizontalalign="Right"
            footerstyle-horizontalalign="Right"
            footerstyle-backcolor="Blue"
            footerstyle-forecolor="White">
            <itemtemplate>
              <%#Eval("Total", "{0:c}") %>
            </itemtemplate>
            <footertemplate>
              <asp:label id="OrderTotalLabel"
                runat="server"/>
            </footertemplate>
          </asp:templatefield>

Along with:

随着:

void OrderGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

      // Get the cell that contains the item total.
      TableCell cell = e.Row.Cells[2];

      // Get the DataBoundLiteralControl control that contains the 
      // data-bound value.
      DataBoundLiteralControl boundControl = (DataBoundLiteralControl)cell.Controls[0];

      // Remove the '$' character so that the type converter works properly.
      String itemTotal = boundControl.Text.Replace("$",  "");

      // Add the total for an item (row) to the order total.
      orderTotal += Convert.ToDecimal(itemTotal);

    }

  }

#2


1  

Use Footer row to do that

使用页脚行来执行此操作

Check this link http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html

查看此链接http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html

in the RowDataBind event check for footer as follows

在RowDataBind事件中检查页脚如下

float _total;
protected void grdv_cart_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  DataRow dr = ((DataRowView)e.Row.DataItem).Row;
  float itemPrice = float.Parse(dr["ItemPrice"].ToString());
  _total += itemPrice;
 }
if (e.Row.RowType == DataControlRowType.Footer)
{
  //here write the totals into labels
}

if any doubts give me a comment!

如果有任何疑问给我评论!

相关文章