0% encontró este documento útil (0 votos)
766 vistas40 páginas

Ejemplo Crystal Report

Este documento describe cómo conectar un informe de Crystal Reports a una colección de objetos en .NET. Primero, se crea una clase Stock que representa datos del mercado de acciones. Luego, se crea un informe en Crystal Reports y se conecta a la clase Stock. Finalmente, se genera una instancia de ArrayList, se agregan objetos Stock, y se asigna el ArrayList como origen de datos del informe, lo que permitirá que el informe se rellene dinámicamente con los datos de la colección.

Cargado por

ateneo0007
Derechos de autor
© Attribution Non-Commercial (BY-NC)
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
766 vistas40 páginas

Ejemplo Crystal Report

Este documento describe cómo conectar un informe de Crystal Reports a una colección de objetos en .NET. Primero, se crea una clase Stock que representa datos del mercado de acciones. Luego, se crea un informe en Crystal Reports y se conecta a la clase Stock. Finalmente, se genera una instancia de ArrayList, se agregan objetos Stock, y se asigna el ArrayList como origen de datos del informe, lo que permitirá que el informe se rellene dinámicamente con los datos de la colección.

Cargado por

ateneo0007
Derechos de autor
© Attribution Non-Commercial (BY-NC)
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 40

http://msdn.microsoft.com/es-es/library/ms225242(v=vs.80).aspx http://msdn.microsoft.com/es-es/library/ms227523(v=vs.80).aspx http://msdn.microsoft.com/es-es/library/ms227408(v=vs.80).aspx conectar a colecciones de objetos http://msdn.microsoft.com/es-es/library/ms227595(v=vs.80).

aspx

Tutorial: Conectar a colecciones de objetos: Introduccin


En este tutorial, crear una clase que es el tipo de cada objeto de la coleccin de objetos. La clase representar informacin del mercado de cotizaciones. Al crear un informe de Crystal, a esta clase Stock se accede a travs del asistente de informes de forma parecida a una tabla de base de datos, pero en lugar de agregar columnas de tabla como campos que se desea mostrar, agregar propiedades. Cuando el informe se muestra por primera vez estar vaco. Se ha terminado el diseo del informe, pero no hay datos con los que llenarlo. A continuacin, crear un mtodo que genere una instancia de ArrayList y agregue varias instancias de Stock a la instancia de ArrayList. Cada instancia de Stock tiene sus propiedades definidas como valores exclusivos. El mtodo devolver entonces la instancia de ArrayList. Agregar esta informacin mediante programacin en tiempo de diseo y de nuevo de manera dinmica en tiempo de ejecucin. El ArrayList devuelto, una coleccin de objetos, se asignar a la propiedad SetDataSource del informe de Crystal. Cuando se muestre el informe, cada objeto de la coleccin de objetos ofrecer una fila de detalle en el informe. Para empezar, crear la clase Stock

Crear la clase Stock


En esta seccin, crear la clase Stock. La clase Stock contiene campos privados que se exponen como propiedades pblicas: Symbol, Volume y Price. Para crear la clase Stock Nota Este procedimiento slo funciona con un proyecto creado a partir de la Configuracin de proyectos. La Configuracin de proyectos contiene referencias especficas a espacios de nombres y configuracin de cdigos necesarios para este procedimiento, y ste no se podr completar sin dicha configuracin. Por lo tanto, antes de que empiece con este procedimiento, primero debe seguir los pasos deConfiguracin de proyectos. 1. 2. En Explorador de soluciones, haga clic con el botn secundario en el nombre de sitio Web que est en negrita y, a continuacin, haga clic enAgregar nuevo elemento. En el cuadro de dilogo Agregar nuevo elemento: En el campo Plantillas instaladas de Visual Studio, seleccione Clase. En el campo Nombre, escriba "Stock" y, a continuacin, haga clic en Agregar. En el cuadro de dilogo que aparece, haga clic en S. Nota En Visual Studio 2005, todas las clases deben estar ubicadas en una carpeta App_Code si se desea que puedan ser utilizadas por todos. Al hacer clic en el botn Agregar, aparece un cuadro de alerta que le pregunta si desea colocar la clase en esta carpetaApp_Code. La clase Stock debe definirse como pblica para que se pueda acceder a ella al crear el informe. Verifique que la clase que ha creado es pblica. Si no lo es, agregue el modificador public a la firma de clase para exponer la clase al Crystal Report Designer incrustado. [Visual Basic]

Public Class Stock End Class [C#] public class Stock { public Stock() { } } 1. Si la codificacin se realiza en Visual Basic, agregue un constructor predeterminado. [Visual Basic] Sub New() End Sub 2. Dentro de la clase, agregue tres campos privados. [Visual Basic] Private _symbol As String Private _volume As Integer Private _price As Double [C#] private string _symbol; private double _price; private int _volume; A continuacin, agregar tres propiedades pblicas de lectura y escritura para encapsular los tres campos privados. 1. Cree una nueva propiedad denominada Symbol. [Visual Basic] Public Property Symbol() As String Get Return _symbol End Get Set(ByVal value As String) _symbol = value End Set End Property [C#] public string Symbol { get { return _symbol; } set

{ _symbol = value; } } 2. Cree una nueva propiedad denominada Price. [Visual Basic] Public Property Price() As Double Get Return _price End Get Set(ByVal value As Double) _price = value End Set End Property [C#] public double Price { get { return _price; } set { _price = value; } } 3. Cree una nueva propiedad denominada Volume. [Visual Basic] Public Property Volume() As Integer Get Return _volume End Get Set(ByVal value As Integer) _volume = value End Set End Property [C#] public int Volume { get { return _volume; } set {

_volume = value; } } 4. Por ltimo, cree un constructor nuevo que considere las tres propiedades pblicas como argumentos. [Visual Basic] Sub New(ByVal symbol As String, ByVal volume As Integer, ByVal price As Double) _symbol = symbol _volume = volume _price = price End Sub [C#] public Stock (String symbol, int volume, double price) { _symbol = symbol; _volume = volume; _price = price; } 5. En el men Crear, haga clic en Generar sitio Web.

Si existen errores de generacin, corrjalos ahora. Ahora podr acceder a este objeto desde el Crystal Report Designer incrustado. Contine con Conectar un informe al objeto Stock.

Conectar un informe al objeto Stock


En esta seccin crear un nuevo informe de Crystal en el Crystal Report Designer incrustado y conectar el informe al objeto Stock. Para conectar un informe de Crystal al objeto Stock 1. 2. 3. 4. 5. Haga clic con el botn secundario en el nombre de proyecto y haga clic en Agregar nuevo elemento. En el cuadro de dilogo Agregar nuevo elemento, seleccione Crystal Report. En el campo Nombre, especifique "StockObjects.rpt" y, a continuacin, haga clic en Agregar. En el cuadro de dilogo Galera de informes de Crystal, haga clic en Aceptar. En el cuadro de dilogo Asistente para la creacin de informes estndar, expanda Datos del proyecto y el subnodo Objetos .NET. Aparecer una lista de clases dentro del proyecto. Cada clase va precedida del espacio de nombres del proyecto. 6. 7. 8. 9. Expanda la clase Stock para ver un subnodo que se puede seleccionar. Haga clic en la flecha derecha para mover el subnodo de la clase Stock al panel Tablas seleccionadas y, a continuacin, haga clic en Siguiente. Expanda Stock y haga clic en el smbolo >> para mover todas las columnas al panel Campos para mostrar y, a continuacin, haga clic en Siguiente. Seleccione Symbol y haga clic en la flecha derecha para mover el campo al panel Agrupar por y, a continuacin, haga clic en Finalizar.

Contine con Enlazar el informe.

Enlazar el informe

En Configuracin de proyectos, va a colocar un control CrystalReportViewer en el formulario Web Forms o Windows Forms. En el procedimiento anterior, agreg un informe StockObjects al proyecto. En esta seccin, enlazar el informe Stock Objects al Visor de informes de Crystal, establecer el origen de datos del informe en una coleccin de objetos y rellener esta ltima mediante programacin. Para crear una copia del informe StockObjects como informe no incrustado y enlazarla al control CrystalReportViewer 1. 2. Abra la clase Code-Behind predeterminada, Default.aspx.cs o Default.aspx.vb. Encima de la firma de clase, agregue una declaracin "Imports" [Visual Basic] o "using" [C#] en la parte superior de la clase para el espacio de nombres System.Collections. [Visual Basic] Imports System.Collections [C#] using System.Collections; Nota Esta referencia le facilita acceso a la clase ArrayList. ArrayList implementa ICollection. Esto cualifica a ArrayListcomo uno de los tipos de clase que se pueden usar para crear una coleccin de objetos que reconozca Crystal Reports. 3. Agregue una nueva ArrayList de nivel de clase y asgnele el nombre stockValues. [Visual Basic] Private stockValues As ArrayList [C#] private ArrayList stockValues; 4. Agregue una nueva declaracin de nivel de clase para la clase contenedora del informe ReportDocument, con el nombre de variablestockObjectsReport. Establezca su modificador de acceso como privado. [Visual Basic] Private stockObjectsReport As ReportDocument [C#] private ReportDocument StockObjectsReport; 5. En el mtodo ConfigureCrystalReports() que cre en Configuracin de proyectos, declare una variable de cadena, asgnele el nombrereportPath y una ruta de tiempo de ejecucin al informe local. Pase el nombre del archivo de informe local como un parmetro de cadena en el mtodo Server.MapPath(). El informe local se asigna a la ruta de archivo en tiempo de ejecucin. [Visual Basic]

Dim reportPath As String = Server.MapPath("StockObjects.rpt") [C#] string reportPath = Server.MapPath("StockObjects.rpt"); 6. Agregue dos saltos de lnea y cree una instancia de la clase ReportDocument. [Visual Basic] stockObjectsReport = New ReportDocument() [C#] stockObjectsReport = new ReportDocument(); 7. En la lnea siguiente, llame el mtodo Load() de la instancia ReportDocument y pselo a la variable de cadena reportPath. [Visual Basic] stockObjectsReport.Load(reportPath) [C#] stockObjectsReport.Load(reportPath); Nota La clase ReportDocument forma parte del espacio de nombres CrystalDecisions.CrystalReports.Engine. Al crear una instancia de ReportDocument y cargar un informe, tendr acceso al informe a travs del SDK. 8. A continuacin, establezca el origen de datos del informe en stockValues ArrayList. [Visual Basic] stockObjectsReport.SetDataSource(stockValues) [C#] stockObjectsReport.SetDataSource(stockValues); 9. Por ltimo, enlace la propiedad ReportSource de CrystalReportViewer a la instancia de ReportDocument. [Visual Basic] myCrystalReportViewer.ReportSource = stockObjectsReport [C#] crystalReportViewer.ReportSource = stockObjectsReport;

En este punto, el informe Stock Objects est enlazado al Visor de informes de Crystal y la pgina muestra el informe correcto; sin embargo, el informe est actualmente enlazado a un origen de datos vaco por lo que no dispone de informacin que mostrar. En la seccin siguiente, rellenar mediante programacin stockValues ArrayList con datos de muestra. Contine con Rellenar la coleccin de objetos mediante programacin.

Rellenar la coleccin de objetos mediante programacin


En esta seccin, agregar Session code a la clase de cdigo subyacente ASPX. Si actualmente no hay ningn valor vigente en la sesin, se crearn valores predeterminados. Si hay valores en la sesin, stos se asignarn a stockValues ArrayList. 1. 2. Haga clic con el botn secundario en el formulario Web del Explorador de soluciones y haga clic en Ver cdigo. En la clase, agregue un nuevo mtodo de ayuda de mbito pblico sin valor de devolucin, denominado PopulateStockValuesArrayList(). [Visual Basic] Public Sub PopulateStockValuesArrayList() End Sub [C#] public void PopulateStockValuesArrayList() { } 3. En el mtodo PopulateStockValuesArrayList(), delante del cdigo existente, cree un bloque condicional if/else que compruebe si existe un objeto Session denominado stockValues. [Visual Basic] If (Session("stockValues") Is Nothing) Then Else End If [C#] if(Session["stockValues"] == null) { } else { } 4. En el bloque If, cree una nueva instancia de ArrayList() [Visual Basic] stockValues = New ArrayList

[C#] stockValues = new ArrayList(); 5. A continuacin, utilice el constructor sobrecargado para la clase Stock y cree tres instancias de Stock. [Visual Basic] Dim s1 As Stock = New Stock("AWRK", 1200, 28.47) Dim s2 As Stock = New Stock("CTSO", 800, 128.69) Dim s3 As Stock = New Stock("LTWR", 1800, 12.95) [C#] Stock s1 = new Stock("AWRK",1200,28.47); Stock s2 = new Stock("CTSO",800,128.69); Stock s3 = new Stock("LTWR",1800,12.95); 6. Agregue estas tres instancias a stockValues. [Visual Basic] stockValues.Add(s1) stockValues.Add(s2) stockValues.Add(s3) [C#] stockValues.Add(s1); stockValues.Add(s2); stockValues.Add(s3); 7. Agregue a la sesin los stockValues ArrayList actualizados. [Visual Basic] Session("stockValues") = stockValues [C#] Session["stockValues"]=stockValues; 8. En el bloque Else, escriba una lnea para asignar los valores actuales vigentes en la sesin a stockValues ArrayList. [Visual Basic] stockValues = Ctype(Session("stockValues"), ArrayList) [C#] stockValues = (ArrayList)Session["stockValues"];

9.

Por ltimo, llame PopulateStockValuesArrayList() desde el mtodo ConfigureCrystalReports(). Debe ser la primera lnea de cdigo que se ejecute en el mtodo ConfigureCrystalReports(). [Visual Basic] PopulateStockValuesArrayList() [C#]

PopulateStockValuesArrayList(); Para probar la carga del informe StockObjects 1. 2. 3. En el men Crear, seleccione Generar solucin. Si existen errores de generacin, contine y corrjalos ahora. En el men Depurar, haga clic en Iniciar depuracin. Nota Si es la primera vez que ha iniciado la depuracin, aparecer un cuadro de dilogo en el que se indica que el archivo Web.config se debe modificar. Haga clic en el botn Aceptar para habilitar la depuracin. Si no aparecen errores de generacin, se cargar la pgina Default.aspx en el explorador con los tres valores predeterminados. 4. Vuelva a Visual Studio y haga clic en Detener para salir del modo de depuracin.

Contine con Rellenar dinmicamente la coleccin de objetos.

Rellenar dinmicamente la coleccin de objetos


En la seccin anterior, rellen la coleccin de objetos mediante programacin. En esta seccin, aprender a agregar informacin dinmicamente al origen de datos desde su sitio Web. Esta informacin se actualizar dinmicamente en el informe. Para agregar controles al formulario Web En esta seccin, agregue los controles al formulario Web necesarios para actualizar dinmicamente la coleccin de objetos. 1. Abra el archivo Default.aspx en la vista Diseo. Nota Para abrir una pgina ASPX en la vista de diseo, primero abra el archivo y despus, haga clic en el botn Diseo situado en la parte inferior del formulario. 2. 3. 4. 5. 6. 7. 8. 9. Haga clic en el control CrystalReportViewer para seleccionarlo. Presione la flecha izquierda del teclado para que aparezca un cursor que parpadea y, a continuacin, presione Intro varias veces. En Cuadro de herramientas, arrastre un control TextBox al formulario Web. En el men Propiedad, establezca el valor de ID en "symbol". Arrastre un segundo control TextBox en el formulario Web. Coloque el segundo TextBox debajo del primero. En el men Propiedad, establezca el valor de ID en "price". Arrastre un tercer control TextBox en el formulario Web. Coloque el tercer TextBox debajo del segundo. En el men Propiedad, establezca el valor de ID en "volume". Nota En este punto, puede resultar til agregar texto junto a cada control TextBox para identificar el control correspondiente a cada parmetro.

10. A continuacin, desde Cuadro de herramientas, arrastre un control Button en el formulario Web. Coloque el botn debajo de los tres controlesTextBox. 11. En el men Propiedad: Establezca el ID del control Button en "addStockInformation". Establezca el texto de Button en "Add Stock Information". 12. Por ltimo, haga doble clic en el botn "Add Stock Information". Al hacer doble clic en el control Button, se abrir la clase Code-behind y se generar automticamente un controlador de eventosaddStockInformation_Click(). Para agregar informacin a la coleccin En esta seccin, escribir el cdigo para el controlador de eventos addStockInformation_Click() que agregar la informacin introducida en el formulario Web a la coleccin stockValues. 1. En el controlador de eventos addStockInformation_Click(), cree una nueva instancia de un objeto Stock. [Visual Basic] Dim temp As Stock = New Stock() [C#] Stock temp = new Stock(); 2. Dentro del mtodo addStockInformation_Click(), cree un bloque try/catch. [Visual Basic] Try Catch ex As Exception End Try [C#] try { } catch { } La informacin introducida en un formulario Web es de tipo cadena. Debido a que dos de los campos de la clase Stock son numricos, tendr que escribir el cdigo para convertir los valores de cadena del formulario Web en valores numricos. El enunciado try/catch permite proteger la aplicacin Web de interrupciones bruscas del sistema durante la conversin debido a errores de incompatibilidad de tipo. 3. En el bloque Try, asigne el valor del campo de smbolo en el formulario Web a la propiedad Symbol del objeto Stock. [Visual Basic] temp.Symbol = symbol.Text

[C#] temp.Symbol = symbol.Text; 4. En la lnea siguiente, asigne el valor del campo de precio en el formulario Web a la propiedad Price del objeto Stock. Convierta el valor del formulario Web en Double antes de la asignacin. [Visual Basic] temp.Price = CType(price.Text, Double) [C#] temp.Price = Convert.ToDouble(price.Text); 5. A continuacin, asigne el valor del campo de volumen en el formulario Web a la propiedad Volume del objeto Stock. Convierta el valor del formulario Web en un nmero entero antes de la asignacin. [Visual Basic] temp.Volume = CType(volume.Text, Integer) [C#] temp.Volume = Convert.ToInt32(volume.Text); 6. Fuera del bloque try/catch, agregue el objeto Stock a stockValues ArrayList. [Visual Basic] stockValues.Add(temp) [C#] stockValues.Add(temp); 7. Actualice el valor stockValues actualmente vigente en la sesin. [Visual Basic] Session("stockValues") = stockValues [C#] Session["stockValues"] = stockValues; 8. Por ltimo, llame al mtodo ConfigureCrystalReports(). El informe se enlazar de nuevo a la coleccin de objetos stockValues actualizada. [Visual Basic] ConfigureCrystalReports()

[C#] ConfigureCrystalReports(); 9. En el men Crear, haga clic en Generar solucin. Si existen errores de generacin, corrjalos ahora. 10. En el men Depurar, haga clic en Iniciar depuracin. Si no aparecen errores de generacin, se cargar la pgina Default.aspx en el explorador con los tres valores predeterminados. Para agregar valores adicionales, rellene los controles TextBox Controls como corresponda y haga clic en el botn Add Stock Information. El informe se actualizar dinmicamente. Al ejecutar el sitio Web, el informe se cargar en la ventana del explorador con los tres valores predeterminados que agreg mediante programacin enRellenar la coleccin de objetos mediante programacin. Encima del informe aparecen tres controles TextBox y un control Button. Mediante estos controles, puede actualizar dinmicamente la coleccin de objetos y ver el resultado de esta actualizacin reflejada en el informe. Contine con Conclusin.

Tutorial: Conectar a colecciones de objetos: Conclusin


Ha creado una clase nica para mantener los valores del mercado de cotizaciones, ha creado una instancia de la clase, ha rellenado una coleccin de objetos con datos y ha agregado de manera dinmica datos adicionales a travs de un formulario Web. Despus, ha creado un informe de Crystal con el control Crystal Report Designer que se conect a la coleccin de objetos y gener dinmicamente un grfico y resumen de cotizaciones. Las colecciones de objetos son orgenes de datos verstiles para los informes de Crystal. Con una coleccin de objetos, puede escribir una clase personalizada que rellena un objeto con datos mediante programacin o dinmicamente. Las colecciones de objetos se pueden utilizar tambin para ampliar el nmero de orgenes de datos que se facilitan con una instalacin predeterminada de Crystal Reports para Visual Studio permitindole escribir sus propios objetos de acceso a datos. Para aprender ms sobre el uso de una coleccin de datos en un informe, contine con Apndice: Mejorar el informe. Contine con Informacin de cdigo de ejemplo.

Generate a Report using Crystal Reports in Visual Studio 2010

Introduction
I was searching the internet for Crystal reports in 2010 and I found in 2005, but was not able to find any particular tutorials for 2010. So I thought, let me take a chance to write it for some beginners like me who have not worked on Crystal Reports earlier. In this article, I will show you simple report creation process with screenshots. A picture is worth more than a thousand words, so I always believe in an article with screenshots. Let's start by creating a new website in VS2010. See the following screen:

Figure 1

As per figure 1, create a new website in VS2010 and name it as per your choice. Now let me show you the database table structure.

Figure 2

The above figure shows the db table structure. And the below figure (Figure 3) will show you some sample data in the table:

Figure 3

If you want to run this sample project directly, then you can download the database script from the link at the top. Now we have to create an xsd file as a blank data source as we are going to use strong data type. Here I will divide this tutorial in 5 sub sections as mentioned below:

Simple report using Crystal Reporting Tool Group report Chart report Sub report Cross tab report

Simple Report using Crystal Report


The below figure shows you the process to create an XSD file.

For adding an XSD file, click on Solution Explorer -> Right Click on Project -> click on Add new Item and then it will show you the below screen.

Figure 4

Click on the ok button, so it will ask for confirmation to put that file in App_Code folder. Just click ok and that file will open in the screen as a blank screen. Now we will add one blank datatable to that XSDfile. Just right click on the file and select Add -> Datatable. It will add one DataTable1 to the screen. Figure 5 shows how to add datatable to XSD file.

Figure 5

Now datatable1 is added to XSD file. Now we will add data column to the datatable1 as per figure 6. Remember whatever fields (columns) we add here, it will be available to show on the report. So add column which you want to display in your reports one by one here.

Figure 6

Remember to give the exact same name for data column as in database and also select data type which is the same as database, otherwise you will get an error for field and data type mismatch. Once we add all the required columns in datatable, then set property for the datacolumn as it has in database. The below figure will show you how to set property for data columns. Default datatype for all the columns is string here so if datatype is other than string then only change it manually. Just right click on the datacolumn in datatable and select property and from property window, select appropriatedatatype from DataType Dropdown for that datacolumn.

Figure 7

That's it. XSD file creation has been done. Now we will move to create Crystal report design. Just click on the Solution Explorer -> Right click on the project name and select crystal reports. Name it as per your choice and hit the add button. Figure 8 will show you the creation process of Crystal reports.

Figure 8

Click on the add button and one .rpt file will be added to the solution. And also, it will ask for the report creation type of how you want to create the report. Figure 9 will show you a screenshot.

Figure 9

Just click ok button to proceed. It will lead you to figure 10:

Figure 10

Under project data, expand ADO.NET Datasets and select DataTable1 and add to the selected table portion located at the right side of the windows using > button. Now click on the Finish button and it will show the next screen (Figure 11):

Figure 11

Once report file is added, you can see Field Explorer on the left side near server explorer. Expand Database Fields, under that you will be able to find Datatable that we have created earlier. Just expand it and drag one by one filed from Field Explorer to the rpt file under detail section. Now the report design part is over. Now we have to fetch the data from database and bind it to dataset and then bind that dataset to the report viewer. Let's go step by step. First Drag a CrystalReportViewer control on aspx page from tool box as per below screen:

Figure 12

Now we will fetch the data, pass data to the dataset and then add that dataset to the Crystal Report. Below is the C# code which will do the job:
Collapse | Copy Code

using using using using using

CrystalDecisions.CrystalReports.Engine; CrystalDecisions.Shared; System.Data; System.Data.SqlClient; System.Configuration;

Below is the final code for reports:


Collapse | Copy Code

protected void Page_Load(object sender, EventArgs e) { ReportDocument rptDoc = new ReportDocument(); dsSample ds = new dsSample(); // .xsd file name DataTable dt = new DataTable(); // Just set the name of data table dt.TableName = "Crystal Report Example"; dt = getAllOrders(); //This function is located below this function ds.Tables[0].Merge(dt); // Your .rpt file path will be below rptDoc.Load(Server.MapPath("../Reports/SimpleReports.rpt")); //set dataset to the report viewer. rptDoc.SetDataSource(ds); CrystalReportViewer1.ReportSource = rptDoc; } public DataTable getAllOrders() { //Connection string replace 'databaseservername' with your db server name

string

sqlCon = "User ID=sa;PWD=sa; server=databaseservername;INITIAL CATALOG=SampleDB;" + "PERSISTSECURITY INFO=FALSE;Connect Timeout=0"; SqlConnection Con = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand(); DataSet ds = null; SqlDataAdapter adapter; try { Con.Open(); //Stored procedure calling. It is already in sample db. cmd.CommandText = "getAllOrders"; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = Con; ds = new DataSet(); adapter = new SqlDataAdapter(cmd); adapter.Fill(ds, "Users"); } catch (Exception ex) { throw new Exception(ex.Message); } finally { cmd.Dispose(); if (Con.State != ConnectionState.Closed) Con.Close(); } return ds.Tables[0]; }

Now just save everything and run report. It will look like the below figure:

Figure 13

Grouping in Crystal Report


Here we will see only report design and rest of the things, you can refer from Section 1. Here we will groupCustomer, Product, Order and Quantity. For details, just go through Figure 14. Just add a report (.rpt) file to the solution. Select the appropriate dataset from popup window. Once it's done, then select grouping option like figure 14.

Figure 14

Now, right click on the report, select Report -> Group Experts and the resulting window will look like figure 15:

Figure 15

Now we want to group like Customer Name and Product name so first add Customer to the right Panel. Then move product name to the right panel like figure 16:

Figure 16

This time Crystal report design will be different than the previous time. See figure 17.

GroupHeaderSection1 and GroupHeaderSection2 are added to the report designer. Here Group #1 Name refers to Customer Name and Group #2 Name refers to Product Name.
And also GroupFooterSection1 and GroupFooterSection2 are added below if you want to add something to group footer.

Figure 17

Now under every group, we want to show the number of orders per customer and productwise, so for that, we have to add summary to the GroupFooterSection2. Refer to Figure 18.

Figure 18

Right Click on the GroupFooterSection select Insert -> Summary. It will show you the next screen (Figure 19). And I have also added Order_ID and Product_Qty field to the detail (section 3) part.

Figure 19

In summary window, select the column which you want to summarize in the first dropdown. Select Sum (First option) from the calculate drop down. Summary Location is already set to the report footer. So just click ok to place that summary field to the report. By default, Summary field is added to the Report Footer section so move it to the groupFooterSection2 if you want to group product wise, move it to the GroupFooterSection1 if you want to group Customer wise or keep it at original place if you want to sum all ordered products. I have moved to the FooterSection1 so it will show Customer Wise Total Quantity. Refer to Figure 20.

Figure 20

Now save the report and run it finally. It looks like figure 21.

Figure 21

Chart in Crystal Report


Chart is the most important and visible part of the reporting tool. Crystal has very powerful feature to add chart in report. Let's see how to add chart in CR. Here also, we will see only designing of the chart for other thing. Please refer to Section 1. Here we will show customer wise product ordered quantity in chart. X portion will display Customer name and Y portion will display customers total ordered quantity. First add charts to the report design. Right click on the .rpt file and select Insert->Chart. Refer to figure 22.

Figure 22

Once you add chart to the report, it will not show chart on the report file but with mouse pointer you can see one blank rectangle is moving. So just click on the Report header. It will open popup for chart style and other options. Refer to figure 23.

Figure 23

Now from type tab, select type of the charts like bar chart, line chart, pie chart, etc. form the left side. Select sub type from the right pane like side by side chart, percentage bar chart, etc. I am not going into the detail of it. I am leaving it for you to practice work. And also select vertical or horizontal radio button from the below section if you want to change the chart style vertically or horizontally. Check Use depth effect check box if you need shadow effect on the graph. Refer to figure 23.

Figure 24

As per figure 24, move to the next tab data. There are three boxes, available fields, on change of and show values. So move Customer Name from available fields to on changes of box, and move Product Quantity filed to the show value box and click ok. Now you can see chart is added to the report header section as per figure 25.

Figure 25

Now, just save the report and run it. You can see a Report as a chart on the screen.

Report Inside Report (Sub Report)


Crystal reports provide reports inside report feature which are normally known as a subreport feature. Let me explain it in detail. Here also, we will design only sub report design. For rest of the things, refer to Section 1. Add new report to the solution. Then add Report->Group and select only Customer name because we want to design report for each customer and sub report product wise. So there will be only one group header inside theCustomergroup header as per figure 26.

Figure 26

Now right click on Detail section and select Insert->Subreport. Refer to figure 27.

Figure 27

Once we add subreport, it will show screen like figure 28.

Figure 28

As per figure 28, by default, choose a Crystal Report in project is selected if you want to add report from the project, then otherwise select create a subreport with the report wizard. Once we select create a subreport with Report Wizard (3rd radio button), we have to click on the Report Wizard button to select report type and data source just do as Part - 1 first. Then click on ok button so like chart report it will show a moving rectangle around mouse, click on the detail section where you want to show subreport. Now to edit the sub report, refer to figure 29.

Figure 29

Click on the edit subreport option and format the report as per your need. Here I will suggest add product name and product quantity or you can add chart also for sub report. When you click on the subreport button, it will open subreport designer, actually CR will create a separate .rpt file but it will remain hidden inside the main .rpt file so we can't see it.. Now run the report and you can see the result, report inside report like figure 30.

Figure 30

Here number 1 is the main report and number 2 is the subreport it's showing title as Product wise.

Cross Tab Report in Crystal Report


First, let me make it clear as to what is a Cross tab report. Normally, we generate report row wise like first we show customer name, then product wise, etc. But suppose we want to see report as column wise like product name should be displayed as column in report, then cross tab report comes into the picture. See result of Cross Tab report in figure 31.

Figure 31

Here also, I will show how to design cross tab report only, for rest of the things, refer to Section 1. First add .rpt file to the solution. Then add cross report to the Report Header section as per the below figure (Refer to figure 32). Remember we can add cross tab report only in Report header or report footer section.

Figure 32

Once we click on cross tab report options, it will show moving rectangle around mouse pointer just place it to the report header section.

As we click on header section, it will lead to the figure 33.

Figure 33

As per the figure, move Customer name field to the Rows section, Product name we want to show as a Column so move it to the Columns fields, and we want to show product total so move it to the summarized fields. That's it. Just run the report and you can see the output as shown in figure 31. It's as simple as that. If you have any queries regarding any type of the above mentioned reports, please let me know by way of comments. I will try my level best to fulfill your request. Let's enjoy reporting!

También podría gustarte