You must Sign In to post a response.
  • Category: ASP.NET

    How to show data in gridview like below manner in asp.net

    i am fetching data from query in below manner
    id amount
    1 1
    1 2
    2 5
    1 3
    3 3
    2 2
    and i want to show data in gridview like below manner

    id totalamount
    1 6
    2 7
    3 3
    Total :16

    please tell me how to display data in this manner in asp.net with c#
    can we fetch data direct in sql server query.
  • #645386
    Create table query,

    CREATE TABLE [dbo].[Sample](
    [ID] [int] NULL,
    [Amount] [int] NULL
    ) ON [PRIMARY]

    GO

    Insert query,

    INSERT INTO [Sample]([ID],[Amount])VALUES(1,1)
    INSERT INTO [Sample]([ID],[Amount])VALUES(1,2)
    INSERT INTO [Sample]([ID],[Amount])VALUES(2,5)
    INSERT INTO [Sample]([ID],[Amount])VALUES(1,3)
    INSERT INTO [Sample]([ID],[Amount])VALUES(3,3)
    INSERT INTO [Sample]([ID],[Amount])VALUES(2,2)
    GO


    Select query to get the desired result

    select id, sum(amount) from sample group by id


    You have to calculate the total by looping through the gridview rows.


    Regards,
    Asheej T K

  • #645393
    you can use the below sql query to fetch the result in desired manner. check below :

    select id, 'TotalAmount'=sum(amount) from TableName group by id

    Then just attach that result set to the gridview.

    For total value you can add the footer of a gridview and show the total value.

    Hope this will help you.

    Thank you.

    If it helps you then Rate this.

    Best Regards,
    Manoranjan Sahoo
    http://www.dotnetsquare.com
    Blog: http://blog.msahoo.net

  • #645396
    Hai Dhruva Singh,

    You can fetch the data as per your requirements- get the id and the sum of amount for the ID. for that you need to use the GROUP BY id.

    So after getting this data, you just bind it to the grid.

    It will solve your issue and will fetch the data according to your requirements.

    Hope it will help.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #645425
    Hi Dhurva singh,


    Use the below for your requirement

    select id, sum(amount) as [totalamount] from YourTableName group by id

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.