Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all - I have a question regarding generating reports from a data table using C# in VS.

On a windows form, the user has a datagridview control filled with various accounting items as column headings (rate, credit card, discount, etc.)

Now, there are about a dozen or so columns that are printed every single time, so they are fixed - that's not a problem. However, the user also has the ability to press a button on that form and insert new columns into the datagridview control, and give them names of their choosing. So depending on what they want to display on the form (which is basically an expense spreadsheet), they might have 12 columns, 17 columns, or whatever.

My idea was that once the form was finished and the user had set up the datagridview control to how they wanted it, I'd scan the datagridview control, read out the rows and columns, and create a data table in memory that could then be fed to Crystal Reports to generate printed output, as well as generate PDFs, CSV files etc. that they require.

This is my problem: using Crystal Reports, you need to know in advance what all the fields are, so you can place them on the report template. Obviously you need to know the SQL statement in advance, too. But if the user is creating the columns at run time, I have no way of knowing in advance what the fields are, so the Crystal Report Designer is useless to me. The only thing I can design on the form is the collection of the dozen fixed columns... I have no way of knowing what's going to be additionally required when the application actually runs.

What I want to do is tell the reporting control, "here, go to this data table, read out all of the columns, and just display however many there are in order from left to right. Don't worry about what they mean, just display whatever columns you happen to find when I invoke you."

Is this doable? I've had a quick look around at other report generators, and they all seem to be the same in that you need to open up a report designer, and placed fixed fields at fixed locations. I don't have all of my fields fixed; only some of them.

Can anybody help me out? This is driving me batty.
Posted
Comments
idenizeni 27-Sep-13 13:31pm    
Not an easy task. I'm not very familiar with Crystal reports. Would it be possible to include all fields in the report and use the fields selected by the user to determine which fields to hide? A bit of a brute force approach that most likely won't work, but your question was so nicely articulated that I felt obligated to say something. I did something similar by taken the user selected fields and dynamically generating a SQL statement from them that was loaded into a GridView which had AutoGenerateColumns enabled.
Fortan_77 27-Sep-13 19:28pm    
That's the problem: I don't know what the fields are going to be, so there's no way you can include all the fields at design time. For example, say the user wants to create a column in the datagridview for "internet expenses", and nobody's done a report for that before, and nobody's going to again, either. How to tell the reporting tool to include this new column on the fly?

Are there any other reporting tools out there which CAN tackle this job?

1 solution

Alright, check this. Closer to what do you want....
Dynamic Crystal Reports from C# Application[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900