473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Pivot table in asp.net gridview control

12
Hi,


I have 4 tables in my database. i want to create pivot table using them in a gridview.

1) Category
CategoryID CategoryName
1 Milk
2 Nuts
3 Fruits
4 Vegetables
5 Cakes


2) SubCategory
SubCategoryID CategoryID SubCategoryName
1 1 Soya
2 2 Almonds
3 2 Cashew
4 3 Apple
5 3 Orange
6 3 Grapes
7 3 Cherry



3) Product
ProductID ProductCode ProductName SubCategoryID Price
1000 A1 Cashew1 3 5
2000 B1 Cashew2 3 5
3000 C1 Cashew3 3 5
4000 D1 BlackGrapes 6 10
5000 E1 GreenGrapes 6 10
6000 F1 RoseGrapes 6 10



4) Sales
ID Date ProductID Qty Cost
1 25-Oct-11 1000 1 5
2 25-Oct-11 2000 1 5
3 25-Oct-11 3000 1 5
4 26-Oct-11 1000 2 10
5 26-Oct-11 2000 2 10
6 26-Oct-11 3000 2 10

------------------------------------------------------------

These are the tables in my DB.

I use the following query to display a pivot table



DECLARE @var nvarchar(500)
DECLARE @str nvarchar(2000)
SELECT @var =
STUFF(
(
select distinct ',[' + ltrim(rtrim(cast(CategoryName as char(10))))+ ']'
from Category
For XML Path('')
),
1,1,'')


SELECT @str = N'
select *
from (
select S.Date, S.Cost, C.CategoryName
from Sales S
left join Product P on S.ProductID = P.ProductID
left join Subcategory SC on SC.SubCategoryID = P.SubCategoryID
left join Category C on C.CategoryID = SC.CategoryID
) DataTable
PIVOT (
Sum(DataTable.Cost)
FOR DataTable.CategoryName IN (' + @var + ')
) PivotTable'



exec sp_executesql @str

It works fine.

It produces the following output.

Date | Cakes | Fruits | Milk | Nuts |Vegetables
2011-10-25 | NULL | NULL | NULL | 15.0000 | NULL
2011-10-26 | NULL | NULL | NULL | 30.0000 | NULL




But, i want to display this in my web page in a GridView as hierarchial data, just like how it will be displayed in a excel pivot table.


That is,



25-Oct-11 15
----Nuts 15
---------Cashew3 15
---------1000 5
---------2000 5
---------3000 5

26-Oct-11 30
----Nuts 30
---------Cashew3 30
---------1000 10
---------2000 10
---------3000 10
Grand Total 45




-----------
I checked this link to display a treeview inside a gridview.
http://www.codeproject.com/KB/aspnet/GridViewTreeView.aspx?msg=2484303

Can anyone please suggest me how to do this to produce a pivot table in a gridview like control, that will be exactly like how pivot table looks in excel spreadsheet.

Thanks in advance
Nov 12 '11 #1
2 8250
dha lak
12
Thanks for the reply.

I will check this link
Nov 14 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Hong Kong Is A Good Place | last post by:
I am using a pivot table control (OWC) via web access to an analysis server. My connection string is: ci.ConnectionString = "Provider=MSOLAP.2;User ID=peterdev01\\" + the_username + ...
2
by: eskil | last post by:
Hi, I am running a pivot table based on a query. I need to be able to display decimal precision for on a calculated field (in the query). The query returns the values correctly (i.e. with the...
0
by: Tim_k | last post by:
Does anyone have an example of how to export a datagrid to an Excel pivot table? The code below exports the grid contents to Excel using the Response object. I'd like to expand it to show the...
3
by: | last post by:
Hello, I have created an ASP.NET 2.0 application that utilized a Gridview Control to display and update/delete data. The problem I am having is that the gridview control is displaying the data...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
3
by: cmay | last post by:
I am trying to make the datalist work for me in presenting data in a "pivot table" manner, but it isn't exactly pretty. I have seen some complex code examples of people trying to re arrange a...
9
by: PeteCresswell | last post by:
I've got something called "Reference Rates". The idea is that on a given day, we have various rates of return for various entities. e.g. Libor 3-month return, Libor 6-month return, US Treasury...
1
by: spitapps | last post by:
In a traditional gridview control the items inserted(bind) into the control are rows and the data is in columns. I have a customer who wants the data fields as rows and the columns as the items...
1
by: thomas.wordsworth | last post by:
This is a bit of a two-part question. 1. Is there a way to control what filters are placed on a pivot chart via vba. I can easily change the underlying query and refresh pivot chart but it's...
2
by: migrant_lad | last post by:
form to view in pivot table view is based on query. form default view is pivot table and will function when the form is opened directly. The problem is that when the form is opened from a control...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.