Click here to Skip to main content
Click here to Skip to main content

SQL Reporting Services with Dynamic Column Reports

By , 8 Aug 2005
 

Report output

Introduction

Hi all, this is the second time I am getting a task for dynamically creating reporting columns. In the first instance, I had done it by first binding the data to a DataGrid (by setting the auto-generated columns feature to true and then exporting to Excel). In the second case, the task was to generate a PDF report.

Then I had to depend on the SQL Reporting Services for creating a dynamic layout.

Background

For basic report creation, please refer to other articles. There are many here in The Code Project.

Using the code

I will explain how to create dynamic columns in this article. Here we go..

Step 1. In my table I have 5 fields (Col1, Col2, Col3, Col4, Col5).

Sample Image

Step .2. I want the output of the report based on the parameter the report receives. For example if 1 is the input to the report it should display Col1 and if the input is 2 then it should display Col2 & Col3 and so on (see the Stored procedure, and you can change it as per your requirement).

Then I have created a stored procedure:

<!--this procedure is for Showing an example and you have 
to follow your own standards and Author is Not giving any guarantee that this 
code will work--> 

CREATE PROCEDURE USP_Dynamic 
@Num int AS 
SET NOCOUNT ON 
IF @Num=1 
Begin 
select Col1 ,'a' as NumRows from tblDynamic 
End 
else IF @Num=2 
Begin select Col2,Col3,'bc' as NumRows from tblDynamic 
End 
else IF @Num=3 
Begin 
select Col3,Col4,Col5,'cde' as NumRows from tblDynamic 
End 
else If @Num=4 
Begin 
select Col1,Col3,Col4,Col5,'acde' as NumRows from tblDynamic 
End else 
Begin 
select Col1,Col2,Col3,Col4,Col5,'abcde' as NumRows from tblDynamic 
End 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

In the stored procedure, if the input is 1 then output will be Col1. It is easy to understand. And the column "NumRows" is for the report to understand the stored procedure output. Like if NumRows="a", then output will contain the first column, if NumRows="ab", then output will contain first column and second column. Logic is that each alphabet will indicate a column. “a” for first, “b” for second, “c” for third, “d” for fourth and “e” for fifth. Any alphabet from the “NumRows” value is missing indicates the absence of that column. Based on this, the report will hide that column.

Step 3. I have created a new report project (In CodeProject, browse for articles on how to create SQL reports).

Sample Image

Step 4. I have added a shared data source to connect to the database TestDB on ServerName.

Sample Image

Step 5. I have created a new report (In Code Project, browse for articles on how to create SQL reports).

Step 6. I have created a dataset (DataSet1) in the report (in Data tab).

Sample Image

Step 7. In Layout tab, I have added all the five (Col1, Col2, Col3, Col4, Col5) fields from the database table manually.

Sample Image

Sample Image

Sample Image

Step 8. I have added a table to the report (if you have a table in the report, skip this step) and dragged all column fields to the table.

Sample Image

Step 9. I have set "DataSet1" to the table.

Sample Image

Step 10. In visibility--Hidden property expression of each table column, I have added the following code to hide that column based on the stored procedure output. In the first column it should be “a”, in second it should be “b”, in third it should be c, in fourth it should be “d”, in fifth it should be “e”. This is for hiding the column based on the stored procedure output. See the stored procedure for more details.

<!--this procedure is for Showing an example and you have 
to follow your own standards and Author is Not giving any guarantee that this 
code will work--> 

//For First Column
=IIF(InStr( Fields!NumRows.Value,"a")=0,True,False)

//For Second Column
=IIF(InStr( Fields!NumRows.Value,"b")=0,True,False)

//For Third Column
=IIF(InStr( Fields!NumRows.Value,"c")=0,True,False)

//For Fourth Column
=IIF(InStr( Fields!NumRows.Value,"d")=0,True,False)

//For Fifth Column
=IIF(InStr( Fields!NumRows.Value,"e")=0,True,False)

Sample Image

Sample Image

Step 11. Done. It is ready for generating dynamic columns. Just preview, it will ask for a number, give 1 or 2 or 3 etc. Your dynamic report is ready. Now you can export it to PDF, Excel or whatever SQL Reporting supports.

Points of Interest

I have learned to dig the SQL Reporting Services with a programmer's shovel.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

JeyKey
Web Developer
United States United States
Member
Jeykey is a software engineer working for an MNC and he has been to programming from 1999.
His programming experience includes Dot net, Java, VB 6.0 SQL AND ASP. He is into playing chess, football, cricket and badminton.He is interested in human psychology and farming.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4 [modified]memberRaviRanjankr4 Dec '11 - 0:47 
[Edited]Link removed[/Edited]
Nice article Thumbs Up | :thumbsup:

modified 5 Dec '11 - 4:33.

QuestionCompletely dynamic solutionmembersonali.mendis15 Jul '11 - 17:17 
Without a knowledge of maximum number of columns dynamic column reports can be generated. Follow http://sonalimendis.blogspot.com/2011/07/dynamic-column-rdls.html#links[^]
GeneralHiding columns based on result setmemberchandu00722 Jan '10 - 4:37 
If columns need to be toggled based on resultset columns when they are not fixed, this is how it can be done.
Each field has a isMissing property which return true if the resultset does not have the column. Check this MSDN link
http://msdn.microsoft.com/en-us/library/cc281297.aspx[^]
GeneralThis in not dynamic!memberHarolds27 Mar '09 - 12:46 
Dynamic would mean you don't know the columns before hand, and the table is built dynamically based on what is returned in the dataset.
GeneralRe: This in not dynamic!memberMycroft Holmes30 Jul '09 - 20:47 
Yah my first reaction - I don't even know the column names let alone the number of columns. Still it points to a possible hack to get it to do what I want. I just wish there was a DGV I could shove into a RDL.
 
Never underestimate the power of human stupidity
RAH

GeneralNeed a helpmemberT. Ravindran17 Jun '08 - 15:20 
Hi,
I am very new to do reports, i need a help.
I am using VS2005.
I want to create 3 reports, 2 reports are displaying same content but other is different.
In that 2 reports one report is having 2 parameter and another one is not having any parameters.
The third one is also having 1 parameters. Now i want to know how many reports i have to deisgn and how to i call these report in my ReportViewer. I have to shown these 3 reports in same form. Pls help me.
GeneralRe: Need a helpmemberBrosten8 Dec '08 - 21:57 
This sounds a bit of topic.
Try the RS forum on MSDN:
http://forums.microsoft.com/msdn/showforum.aspx?siteid=1&forumid=82[^]
QuestionI need to create Dynamic tables. Pl. helpmemberPrashant.IGT13 Jun '08 - 0:04 
My requirment is
 
I am having 4 differents report that needs to create on SSRS. These are
1. Car Reports
2. Car/Date Reports
3. Property Reports
4. Property/Date Reports
 
I am having a SP that runs on the base of above selections.
And it returns different values. (An @id will pass that carry value 1 to 4).
Now what I need to do is user will select any one of the above mention 4 reports from a drop down. On the base of the selection the report will display.
So, I am taking 4 matrix and bind them on the base of above selection. But the problem is to desiging the matrix.
 
Please help?
QuestionHow to take server field that come from the two dataset into one tablemembersdtysdu22 Sep '06 - 7:47 
HI, I Want to ask a question about report design.In the report project layout , I create two datasource ,one come from SQL2005,and the other come from Oracle. Next ,I create two dataset refers to the two datasource. when I take serveral field that come from the two dataset into one table ,and then preview ,the system clue to an error .why ! Because in the table's
property ,dataset'selection is default by the dataset that first field was take into the table. The table can not involve Two dataset.
But table of the report project that I am desiging must involve server field that come from the two dataset .
How to take server field that come from the two dataset into one table???
Can you help me !!!Thank you very much!!!
GeneralRe: How to take server field that come from the two dataset into one tablemembermreynolds12327 Oct '06 - 4:09 
This is an issue I am having as well. Any solutions to this problem would be greatly appreciated.
 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 9 Aug 2005
Article Copyright 2005 by JeyKey
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid