![]() |
Languages »
C# »
Windows Forms
Intermediate
Reporting in Windows.FormsBy pablojagClean, fast and easy way to deliver data from objects |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Sometimes Crystal reports is not easy to use and it does not deliver output just the way we want.
To work around this problem, we just need an HTML template file and a parser class that reads this file and calls the appropriate methods in our classes to deliver dynamic data.
The application posted in this article contains a Windows Forms project
written in C#. It includes a kind of dumy class Personswhich groups attributes like Name, Last Name, Bitrh,
gender, email and country. The data contained in the objects of this class is
the one we want to print in an HTML report. It is only a structure, but I think
it works to explain the solution.
The last class HTMLReport parses the HTML Template file
in its Generate method and calls the Report(string data) of the object passed as an argument every
time it finds a string with the form "<@Data_To_Report>". This string must
be included as a case in switch instruction of the Report
Method.
For example, if the parser finds in the HTML template the code:
<@FullName>
The parser will get <@FullName> and send it as a parameter to the
method Report() of the class Person which has
some code like this:
switch (data)
{
case"<@FullName>":
return mName + " " + mLast;
....
....
....
}
Finally, the parser will make a new HTML file with the html design it found
in the template and the string the class Person returns.
Just like this:
<h2><Somebody's name Somebody's last name</h2>
The idea of Form1.cs is only to illustrate a GUI that fills data in
the Person class. The event click of the button has this
code:
//Create the object person and assign the values
Persons newPerson = new Persons();
newPerson.Name = txtName.Text;
newPerson.Last = txtLast.Text;
newPerson.Country = comboCountry.SelectedItem.ToString();
newPerson.IsMale = radMale.Checked;
newPerson.Email = txtEmail.Text;
newPerson.BirthDate = Convert.ToDateTime( txtBirthDate.Text);
//Create the object Report and assign object to report
HTMLReport html = new HTMLReport();
//Assign the template file
html.Template = "GeneralTemplate.htm";
//Asign the new file name
html.FileName = newPerson.Name;
//Generate the new Page and open it. It uses default browser
System.Diagnostics.Process.Start(html.Generate(newPerson));
One of the biggest improvements of this aproach is that whenever you want to change the format or the order reported data, it is as easy as to edit an HTML page. There are a lot of HTML editors we can use, but I still prefer notepad.
More than a solution to a specific problem, I see this as a framework. It means you should read this article as a way to work whenever you need to deliver data from your objects in a nice, cheap and clean way. Clearly, you must rewrite the code to your specific needs, but you could follow the same pattern for printing data from your objects.
This is just version 1.0. next time I will try to illustrate an example of objects with complex data types like typed Datasets.
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Nov 2004 Editor: Nishant Sivakumar |
Copyright 2004 by pablojag Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |