locked
Printing the contents of a DataGridView using a Crystal report ! ! !

    Question

  • i want to print the contents of a datagridview using a crystal eport........
    How to go or this?
    Wednesday, September 16, 2009 12:55 AM

Answers

  • There are two methods for this...

    First Method:
    if you are using a datatable as datasource for the datagridview control then...

    1. Create a crystal report with all columns available in the datagridview.

    2. Assign the same datatable used for datagridview as datasource to the crystal report.


    Second Method:
    if you are manually populating the datagridview

    1. Create a crystal report with all the columns available in the datagridview.  If some columns are calculated and populated by you then you can use some thing 
        like this (select eno, ename, ebasic, 0 as eta, 0 as eda, 0 as ehra from temporarytable) assuming eta, eda, ehra are calculated by you.

    2. In the program create a datatable object.  Manually add all the columns in the datagridview to the datatable object.

    3. Manually add all rows in datagridview to the datatable.

    4. Assign the datatable object as datasource to crystal report.

    Third Method:
    Dynamic Crystal Report Generation.  Adding columns to crystal report manually. 

    I don't know how to do it.  I searched the internet done a lot of reasearch on it..... but not yet successful.



    Second method I am following when I got the same situation

    Hope this will help you.

    Sharp Eyes

    • Proposed as answer by Harry Zhu Thursday, September 17, 2009 2:50 AM
    • Marked as answer by Harry Zhu Monday, September 28, 2009 7:56 AM
    Wednesday, September 16, 2009 5:56 AM
  • Hi,

    For the first method, you just need create a typed dataset with the colomns of the datagridview,  and put the data of datasource of datagridview into dataset them show them on report, the tutorial shows how to show data of typed dataset :
    http://devlibrary.businessobjects.com/BusinessObjectsXIR2/en/en/CrystalReports_dotNET_SDK/crsdk_net_doc/doc/crsdk_net_doc/html/crtsktutorialsdatads.htm

    For the second on, you might need to retrieve the data of the datagridview them push them into a typed dataset then show them.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Monday, September 28, 2009 7:56 AM
    Tuesday, September 22, 2009 3:19 AM
  • Check the sample code....

            With DataGridView1
                .SuspendLayout()
                .Visible = False
                Application.DoEvents()
    
                .Columns.Add("ENO", "ENO")
                .Columns.Add("ENAME", "ENAME")
    
                Dim I As Integer
    
                For I = 0 To 10 Step 1
                    Dim NewRow As Integer
                    NewRow = .Rows.Add()
                    .Rows(NewRow).Cells("ENO").Value = I
                    .Rows(NewRow).Cells("ENAME").Value = "A" & I
                Next I
    
                .Visible = True
                .ResumeLayout()
                Application.DoEvents()
    
                Dim DT As New DataTable("EMPLOYEES")
                DT.Columns.Add("ENO", I.GetType(), 0)
                DT.Columns.Add("ENAME", "".GetType(), "")
    
                For I = 0 To 10 Step 1
                    Dim D As DataRow
                    D = DT.NewRow()
                    D.Item("ENO") = DataGridView1.Rows(I).Cells("ENO").Value()
                    D.Item("ENAME") = DataGridView1.Rows(I).Cells("ENAME").Value()
                    DT.Rows.Add(D)
                Next I
    
                ''CREATE A CRYSTAL REPORT WITH FIELD ENO AND ENAME 
                ''ASSIGN THIS DATATABLE TO CRYSTAL REPORT
                ''LIKE    CRYSTALREPORT1.DATASOURCE = DT
    
            End With
    

    Create new form ... copy this code in the constructor of the form... add datagridview  to the form... create a crystal report with eno and ename as fields....

    Sharp Eyes

    • Marked as answer by Harry Zhu Monday, September 28, 2009 7:56 AM
    Tuesday, September 22, 2009 7:48 AM

All replies

  • There are two methods for this...

    First Method:
    if you are using a datatable as datasource for the datagridview control then...

    1. Create a crystal report with all columns available in the datagridview.

    2. Assign the same datatable used for datagridview as datasource to the crystal report.


    Second Method:
    if you are manually populating the datagridview

    1. Create a crystal report with all the columns available in the datagridview.  If some columns are calculated and populated by you then you can use some thing 
        like this (select eno, ename, ebasic, 0 as eta, 0 as eda, 0 as ehra from temporarytable) assuming eta, eda, ehra are calculated by you.

    2. In the program create a datatable object.  Manually add all the columns in the datagridview to the datatable object.

    3. Manually add all rows in datagridview to the datatable.

    4. Assign the datatable object as datasource to crystal report.

    Third Method:
    Dynamic Crystal Report Generation.  Adding columns to crystal report manually. 

    I don't know how to do it.  I searched the internet done a lot of reasearch on it..... but not yet successful.



    Second method I am following when I got the same situation

    Hope this will help you.

    Sharp Eyes

    • Proposed as answer by Harry Zhu Thursday, September 17, 2009 2:50 AM
    • Marked as answer by Harry Zhu Monday, September 28, 2009 7:56 AM
    Wednesday, September 16, 2009 5:56 AM
  • hi sharp eyes,
    can you pleae give me the code for creating the crystal reports and printing the contents of the DataGridView?
    Tuesday, September 22, 2009 12:56 AM
  • Hi,

    For the first method, you just need create a typed dataset with the colomns of the datagridview,  and put the data of datasource of datagridview into dataset them show them on report, the tutorial shows how to show data of typed dataset :
    http://devlibrary.businessobjects.com/BusinessObjectsXIR2/en/en/CrystalReports_dotNET_SDK/crsdk_net_doc/doc/crsdk_net_doc/html/crtsktutorialsdatads.htm

    For the second on, you might need to retrieve the data of the datagridview them push them into a typed dataset then show them.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Monday, September 28, 2009 7:56 AM
    Tuesday, September 22, 2009 3:19 AM
  • Check the sample code....

            With DataGridView1
                .SuspendLayout()
                .Visible = False
                Application.DoEvents()
    
                .Columns.Add("ENO", "ENO")
                .Columns.Add("ENAME", "ENAME")
    
                Dim I As Integer
    
                For I = 0 To 10 Step 1
                    Dim NewRow As Integer
                    NewRow = .Rows.Add()
                    .Rows(NewRow).Cells("ENO").Value = I
                    .Rows(NewRow).Cells("ENAME").Value = "A" & I
                Next I
    
                .Visible = True
                .ResumeLayout()
                Application.DoEvents()
    
                Dim DT As New DataTable("EMPLOYEES")
                DT.Columns.Add("ENO", I.GetType(), 0)
                DT.Columns.Add("ENAME", "".GetType(), "")
    
                For I = 0 To 10 Step 1
                    Dim D As DataRow
                    D = DT.NewRow()
                    D.Item("ENO") = DataGridView1.Rows(I).Cells("ENO").Value()
                    D.Item("ENAME") = DataGridView1.Rows(I).Cells("ENAME").Value()
                    DT.Rows.Add(D)
                Next I
    
                ''CREATE A CRYSTAL REPORT WITH FIELD ENO AND ENAME 
                ''ASSIGN THIS DATATABLE TO CRYSTAL REPORT
                ''LIKE    CRYSTALREPORT1.DATASOURCE = DT
    
            End With
    

    Create new form ... copy this code in the constructor of the form... add datagridview  to the form... create a crystal report with eno and ename as fields....

    Sharp Eyes

    • Marked as answer by Harry Zhu Monday, September 28, 2009 7:56 AM
    Tuesday, September 22, 2009 7:48 AM
  • Hi Sharp Eyes,

    I have a datagrid view which is populated manually, and i need to print the datagridview content to crystalreport.

    Could you please send some sample code using C#.net .

    Thanks in advance:)

     

    Tuesday, March 29, 2011 5:06 PM