【问题标题】:asp.net gridview sum and quantity of the columnsasp.net gridview 列的总和和数量
【发布时间】:2018-08-05 07:06:20
【问题描述】:

我在汇总列的总值时遇到了一个小问题。我有一个购物车,我想在其中总结商品的数量和价格。我已经总结了价格,但是当我试图将它乘以数量时它不起作用。

。 请给我一个建议。

这是一个网格视图:

<asp:GridView ID="GridView1" runat="server" HorizontalAlign="Center" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" DataKeyNames="id"  ShowFooter="true" ShowHeaderWhenEmpty="true">
    <Columns>
        <asp:BoundField DataField="item" HeaderText="Item" SortExpression="item"></asp:BoundField>
        <asp:BoundField DataField="BEE" HeaderText="Description" SortExpression="BEE"></asp:BoundField>
        <asp:BoundField DataField="count" HeaderText="Quantity" SortExpression="count"></asp:BoundField>
        <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price"></asp:BoundField>
        <asp:TemplateField>
            <FooterTemplate>
                <asp:Label ID="lbltxtTotal" runat="server" Text="Total Price" />
            </FooterTemplate>
            <FooterTemplate>
                <asp:Label ID="lblTotal" runat="server" />
             </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="id" HeaderText="id" SortExpression="id"></asp:BoundField>
        <asp:ButtonField CommandName="Delete" Text="Delete" ButtonType="Button" ShowHeader="True" HeaderText="Delete"></asp:ButtonField>

    </Columns>
    <EmptyDataTemplate>No Record Available</EmptyDataTemplate>
    <FooterStyle BackColor="White" Font-Bold="True"></FooterStyle>
    <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

    <PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

    <SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

    <SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

    <SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

    <SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
</asp:GridView>

这是一个后端:

protected void Timer1_Tick(object sender, EventArgs e)
    {
        string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        string Username_new = Username.Replace("APAC\\", "");
        GridView1.DataBind();
        //live data
        String myquery = "Select * from Basket where Username='" + Username_new + "'";
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = myquery;
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        da.Fill(dt);
        GridView1.FooterRow.Cells[1].Text = "Total Amount";
        GridView1.FooterRow.Cells[2].Text = dt.Compute("Sum(price)", "").ToString();
    }

【问题讨论】:

  • "...我遇到了一个错误。"如果您描述了您遇到的错误,我们会为您提供帮助。
  • @VDWWD 和那个话题有什么相似之处?
  • 因为在您的问题中,您有以下问题:“Sys.WebForms.PageRequestManagerServerErrorException:对象引用未设置为对象的实例。”。但它已在编辑中被删除。

标签: c# asp.net gridview multiple-columns rows


【解决方案1】:

这很容易。 在代码中添加方法:

  protected void getSUM()
{
// SQL query that gets total of product sales where category id = 1
string SqlQuery = @"SELECT SUM(ProductSales) AS TotalSales 
  FROM [NORTHWIND].[dbo].[Sales by Category] 
  WHERE CategoryID = 1";

// Declare and open a connection to database
SqlConnection conn = new SqlConnection(
ConfigurationManager.ConnectionStrings["NorthwindConnStr"].ConnectionString);
conn.Open();

// Creates SqlCommand object
SqlCommand comm = new SqlCommand(SqlQuery, conn);

// Gets total sales
decimal TotalSales = Convert.ToDecimal(comm.ExecuteScalar());

// Close connection
conn.Close();
conn.Dispose();
comm.Dispose();

// Adds formatted output to GridView footer
GridView1.Columns[1].FooterText = String.Format("{0:c}", TotalSales);
}

在标记代码中,我们将定义页脚模板并调用 getSUM 方法:

  <FooterTemplate>
  Total sales:   <%# getSUM(); %>
  </FooterTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多