运行Total Gridview

时间:2017-11-28 09:25:34

标签: c# asp.net gridview cumulative-sum

任何想法如何获得gridview中每行的运行总计(累计总数?)? gridview还显示了由下拉列表选择的apt / villa的记录。记录按Trans_ID排序。

这是代码:

<table cellspacing="1" class="auto-style1">
    <tr>
        <td class="auto-style22">
            <strong>
            <asp:Label ID="Label1" runat="server" Text="Apt/Villa" 
CssClass="auto-style10" BackColor="White" Font-Bold="True" Font-Names="Arial 
Narrow"></asp:Label>
            </strong>
        </td>
        <td class="auto-style21">
            <asp:DropDownList ID="DropDownList1" runat="server" 
DataSourceID="RecordPaymentAptDropDown" DataTextField="Apt" 
DataValueField="Apt_ID" AutoPostBack="True" 
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="auto-
style19" BackColor="#CC99FF">
            </asp:DropDownList>
            <asp:SqlDataSource ID="RecordPaymentAptDropDown" runat="server" 
ConnectionString="<%$ ConnectionStrings:db71ConnectionString2 %>" 
SelectCommand="SELECT [Apt], [Apt_ID] FROM [Apartments]">
</asp:SqlDataSource>
        </td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style22">
            <strong>
            <asp:Label ID="Label2" runat="server" Text="Date" 
CssClass="auto-style10" BackColor="White" Font-Bold="True" Font-Names="Arial 
Narrow"></asp:Label>
            </strong>
        </td>
        <td class="auto-style21">
            <asp:TextBox ID="TextBox4" runat="server" TextMode="Date" 
ToolTip="Enter date in fromat: dd/mm/yyyy" CssClass="auto-style6" 
BackColor="#CC99FF"></asp:TextBox>
        </td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style22">
            <strong>
            <asp:Label ID="Label3" runat="server" Text="Description" 
CssClass="auto-style10" BackColor="White" Font-Bold="True" Font-Names="Arial 
Narrow"></asp:Label>
            </strong>
        </td>
        <td class="auto-style21">
            <asp:DropDownList ID="DropDownList2" runat="server" 
 DataSourceID="RecordPaymentDescriptionDropDown" DataTextField="Description" 
 DataValueField="Desc_ID" CssClass="auto-style19" BackColor="#CC99FF">
            </asp:DropDownList>
            <asp:SqlDataSource ID="RecordPaymentDescriptionDropDown" 
runat="server" ConnectionString="<%$ ConnectionStrings:db71ConnectionString2 
%>" SelectCommand="SELECT [Description], [Desc_ID] FROM [Descr]">
</asp:SqlDataSource>
        </td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style24">
            Income/Expenses</td>
        <td class="auto-style21">
            <asp:DropDownList ID="DropDownList3" runat="server" 
CssClass="auto-style19" BackColor="#CC99FF">
                <asp:ListItem>Please choose</asp:ListItem>
                <asp:ListItem Value="1">Income</asp:ListItem>
                <asp:ListItem Value="2">Expenses</asp:ListItem>
            </asp:DropDownList>
        </td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style23">
            <asp:Label ID="Label8" runat="server" BackColor="White" 
CssClass="auto-style10" Font-Bold="True" Font-Names="Arial Narrow" 
Text="Amount"></asp:Label>
        </td>
        <td class="auto-style11">
            <asp:TextBox ID="TextBox1" runat="server" Width="150px" 
CssClass="auto-style6" BackColor="#CC99FF"></asp:TextBox>
        </td>
        <td class="auto-style12"></td>
    </tr>
    <tr>
        <td class="auto-style22">
            <strong>
            <asp:Label ID="Label6" runat="server" Text="Comment" 
CssClass="auto-style10" BackColor="White" Font-Names="Arial Narrow">
</asp:Label>
            </strong>
        </td>
        <td class="auto-style21">
            <asp:TextBox ID="TextBox3" runat="server" Height="58px" 
TextMode="MultiLine" Width="344px" CssClass="auto-style6" 
BackColor="#CC99FF"></asp:TextBox>
        </td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style22">
            &nbsp;</td>
        <td class="auto-style21">
            &nbsp;</td>
        <td class="auto-style20">
            &nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style22">&nbsp;</td>
        <td class="auto-style21">
            &nbsp;</td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style22">
            <asp:Button ID="Button1" runat="server" Text="Save" 
OnClick="Button1_Click" CssClass="auto-style6" BackColor="#000099" Font-
Bold="True" Font-Size="Large" ForeColor="Lime" Height="33px" Width="98px" />
        </td>
        <td class="auto-style21">
            &nbsp;</td>
        <td class="auto-style20">&nbsp;</td>
    </tr>
    <tr>
        <td colspan="3" class="auto-style9">
            <asp:SqlDataSource ID="SqlDataSourceRecordPayment1" 
runat="server" ConnectionString="<%$ ConnectionStrings:db71ConnectionString2 
%>" DeleteCommand="DELETE FROM Trans WHERE Trans_ID = @Trans_ID" 
SelectCommand="SELECT Apt, Date, Income, Expenses, Description, Trans_ID, 
Memo, OwnerName FROM TransApt WHERE (Apt_ID = @Apt_ID)">
                <DeleteParameters>
                    <asp:Parameter Name="Trans_ID" />
                </DeleteParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" 
Name="Apt_ID" PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" 
BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="Trans_ID" 
DataSourceID="SqlDataSourceRecordPayment1" GridLines="Horizontal" 
CssClass="auto-style6" Width="100%" OnRowDataBound="GridView1_RowDataBound" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ShowFooter="True">
                <AlternatingRowStyle BackColor="#F7F7F7" />
                <Columns>
                    <asp:BoundField DataField="Trans_ID" 
HeaderText="Trans_ID" SortExpression="Trans_ID" ReadOnly="True" />
                    <asp:CommandField ShowDeleteButton="True" />
                    <asp:BoundField DataField="Apt" HeaderText="Apt" 
SortExpression="Apt" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="OwnerName" 
HeaderText="OwnerName" SortExpression="OwnerName" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Date" HeaderText="Date" 
SortExpression="Date" DataFormatString="{0:dd/MM/yyyy}" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Description" 
HeaderText="Description" SortExpression="Description" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Income" HeaderText="Income" 
SortExpression="Income" DataFormatString="{0:n}" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Expenses" 
HeaderText="Expenses" SortExpression="Expenses" DataFormatString="{0:n}" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Memo" HeaderText="Comment" 
SortExpression="Memo" >
                    <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                </Columns>
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" 
ForeColor="#F7F7F7" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" 
HorizontalAlign="Right" />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" 
ForeColor="#F7F7F7" />
                <SortedAscendingCellStyle BackColor="#F4F4FD" />
                <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                <SortedDescendingCellStyle BackColor="#D8D8F0" />
                <SortedDescendingHeaderStyle BackColor="#3E3277" />
            </asp:GridView>
            <br class="auto-style6" />
        </td>
    </tr>
</table>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HHCondominium
{
public partial class RecordPayment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("Login.aspx", true);
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, 
EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, 
EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection vid = new SqlConnection("Data Source = s1.XXXX.com; 
Initial Catalog = db71; Persist Security Info = True; User ID = XXXX; 
Password = XXXXX");

        if (DropDownList3.SelectedValue == "1")
        {
            SqlCommand xp = new SqlCommand("Insert into Trans(Apt_ID, Date, 
 Description_ID, Income, Memo) Values (@Apt_ID, @Date, @Description_ID, 
@Income, @Memo)", vid);
            xp.Parameters.AddWithValue("@Apt_ID", DropDownList1.Text);
            xp.Parameters.AddWithValue("@Date", TextBox4.Text);
            xp.Parameters.AddWithValue("@Description_ID", 
 DropDownList2.Text);
            xp.Parameters.AddWithValue("@Income", TextBox1.Text);
            xp.Parameters.AddWithValue("@Memo", TextBox3.Text);

            vid.Open();
            xp.ExecuteNonQuery();
            vid.Close();


        }

        else if (DropDownList3.SelectedValue == "2")
        {

            SqlCommand xp = new SqlCommand("Insert into Trans(Apt_ID, Date, 
Description_ID, Expenses, Memo) Values (@Apt_ID, @Date, @Description_ID, 
@Expenses, @Memo)", vid);
            xp.Parameters.AddWithValue("@Apt_ID", DropDownList1.Text);
            xp.Parameters.AddWithValue("@Date", TextBox4.Text);
            xp.Parameters.AddWithValue("@Description_ID", 
DropDownList2.Text);
            xp.Parameters.AddWithValue("@Expenses", TextBox1.Text);
            xp.Parameters.AddWithValue("@Memo", TextBox3.Text);

            vid.Open();
            xp.ExecuteNonQuery();
            vid.Close();
        }

        {
            string msgstring = "Your data has been saved";
            string content = "window.onload=function(){ alert('";
            content += msgstring;
            content += "');";
            content += "window.location='";
            content += Request.Url.AbsoluteUri;
            content += "';}";
            ClientScript.RegisterStartupScript(this.GetType(), 
"SuccessMessage", content, true);

        }



    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

    { }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs 
e)
    {

    }
    Double totalIncome = 0;
    Double totalExpenses = 0;
    protected void GridView1_RowDataBound(object sender, 
 GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)

        {
            totalIncome += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, 
"Income"));
            totalExpenses += 
Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Expenses"));

        }

        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[5].Text = "Grand Total";
            e.Row.Cells[5].Font.Bold = true;

            e.Row.Cells[6].Text = totalIncome.ToString();
            e.Row.Cells[6].Font.Bold = true;

            e.Row.Cells[7].Text = totalExpenses.ToString();
            e.Row.Cells[7].Font.Bold = true;

            e.Row.Cells[8].Text = (totalIncome-totalExpenses).ToString();
            e.Row.Cells[8].Font.Bold = true;
        }





    }
 }
}

我想要实现的目标是: enter image description here

我尝试过很多我在互联网上找到但没有运气的东西。

谢谢你们,

1 个答案:

答案 0 :(得分:0)

您可以在RowDataBound事件中执行此操作。首先,将事件添加到GridView并添加$(window).load(function () { $('.selectpicker').selectpicker('refresh') }); 以显示总值。

Label

然后在代码背后

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Balance">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>