Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

c# - How to produce sum of each column in sql?

i am trying to do column wise addition in Gridview but couldn't.

like i am filling gridview from store procedure which gives these values .

DateTime 10Quantity 20Quantity 30Quantity 10Amount 20Amount 30Amount GrandTotal
03/10/2015  792      0           594        7920        0     17820   25740
03/10/2015  332      3           194        9990        0     28220   38739

Expected:

DateTime 10Quantity 20Quantity 30Quantity 10Amount 20Amount 30Amount GrandTotal
03/10/2015  792      0           594        7920        0     17820   25740
03/10/2015  332      3           194        9990        0     28220   38739
Totals      1124     3           788        17910       0     46040   64479

i want a new row 'Totals' which should represent total for each column i.e. addition.

Code:

<asp:GridView ID="GridViewDenominationsWiseTransactions" runat="server" Width="100%" AutoGenerateColumns="False"
                        ShowFooter="True" OnRowDataBound="GridViewDenominationsWiseTransactions_RowDataBound"  CssClass="table table-hover table-striped table-bordered">
                        <Columns>
                            <asp:BoundField HeaderText="Date" DataField="DateTime" DataFormatString="{0:dd/MMM/yyyy hh:mm}" HtmlEncode="false"  ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="11%" />
                            </asp:BoundField>
                           <%-- <asp:BoundField HeaderText="Conductor Name" DataField="ConductorName" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%"></ItemStyle>
                            </asp:BoundField>--%>
                            <asp:BoundField HeaderText="10 Quantity" DataField="10Quantity" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="20 Quantity" DataField="20Quantity" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="30 Quantity" DataField="30Quantity" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="10 Amount" DataField="10Amount" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="20 Amount" DataField="20Amount" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="30 Amount" DataField="30Amount" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="8%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField HeaderText="GrandTotal" DataField="GrandTotal" ItemStyle-CssClass="visible-desktop"
                                HeaderStyle-CssClass="visible-desktop">
                                <HeaderStyle CssClass="visible-desktop"></HeaderStyle>

                                <ItemStyle Width="13%" CssClass="visible-desktop"></ItemStyle>
                            </asp:BoundField>
                        </Columns>
                    </asp:GridView>

SP;

ALTER PROCEDURE [dbo].[ReportDenominationWiseTransaction] '03-10-2015', '03-10-2015'
@FromDate date,
@ToDate date
AS
    BEGIN   
        with myquery ([DateTime],amount,Quantity) 
        as
        (
            select  Convert(varchar(20),serverdatetime,101) 'DateTime',amount,1 'Quantity'
                 from tickets
                 WHERE CONVERT(DATE,ServerDateTime) BETWEEN @FromDate and @ToDate
        )
        select *, 10 * c.[10Quantity] '10Amount',20 * c.[20Quantity] '20Amount',30 * c.[30Quantity] '30Amount',((10 * c.[10Quantity])+(20 * c.[20Quantity])+(30 * c.[30Quantity]))'GrandTotal' from (
        SELECT DateTime,[10] AS '10Quantity', [20] AS '20Quantity', [30] AS '30Quantity'
        FROM 
        (SELECT [DateTime], amount,quantity
        FROM myquery) p
        PIVOT
        (
        COUNT ([Quantity])
        FOR [Amount] IN
        ( [10], [20], [30])
        ) AS pvt
        ) as c
    END

i must do it from front end i.e. gridview

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the RowDataBound event handler to evaluate data (sum columns) when they are added to the GridView.

And add a footer to show the totals.

Take a look at this: https://msdn.microsoft.com/en-us/library/bb310552.aspx STEP 3: solution 2. That is how you use an eventhandler to run totals in a GridView.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...