Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my datagridview(dgvCart)'s data are populated by values from textboxes, it's not binded in a database.

do i need to create a dataset for my datatable..? that datatable's values will come from the datagridview(dgvCart). so that i can print it into the crystal report.

or is it possible that dgvCart can be directly printed in the crystal report?

i have created the below code on the module..
i don't know if it is correct.

What I have tried:

VB
Public dSet As New DataSet

    Public Sub createDataSet()
        dSet.Clear()
        frmOrder.dgvCart.DataSource = dSet
        frmOrder.dgvCart.DataMember = "tblCart"

        Dim dTable As New DataTable("tblCart")
        For Each dgvCol As DataGridViewColumn In frmOrder.dgvCart.Columns
    
            dTable.Columns.Add("Item ID", GetType(System.String))
            dTable.Columns.Add("Item Name", GetType(System.String))
            dTable.Columns.Add("Price", GetType(System.Double))
            dTable.Columns.Add("Quantity", GetType(System.Int32))
            dTable.Columns.Add("Sum Total", GetType(System.Double))
            dSet.Tables.Add(dTable)
        Next

        For Each dgvRow As DataGridViewRow In frmOrder.dgvCart.Rows
            Dim dRow As DataRow = dTable.NewRow
            For Each cell As DataGridViewCell In dgvRow.Cells
                dRow(0) = cell.Value
            Next
        Next
Posted
Updated 21-Mar-18 20:46pm
v2

You don't "print a DGV in CR". You create your report in CR with the layout you want (use a table in the report) and set the data source to the same data as your DGV. You then print the report, NOT the DGV.
 
Share this answer
 
Comments
Kim Gabinete 18-Mar-18 11:40am    
yes, that's exactly what i want to. i just can't explain it well in english. sorry for my terms. anyway thank you so much for the info, but i don't know how to create/put a table in the report. how to do it?
Kim Gabinete 18-Mar-18 12:35pm    
like an example on this link.
https://postimg.org/image/fsnrsiy1h/
i also want to include labels and get the values from textboxes, like the total amount and total VAT. pls help me
Mycroft Holmes 18-Mar-18 18:37pm    
You need something like https://www.google.com.sg/search?q=Crystal+reports+tutorial&rlz=1C1CHBF_enSG709SG709&oq=Crystal+reports+tutorial&aqs=chrome..69i57j0l5.13277j0j8&sourceid=chrome&ie=UTF-8
'Insert this code in a button (Print Button)

Dim Querry1 As String = "SELECT Item_ID,Item_Name,Price,Quantity,Sum_Total  From tblCart Where TransID = '" & TransID.Text & "' 
            SQL.runQuery(Querry1)

            Dim rptDocs1 As ReportDocument
            Dim connection As New ConnectionInfo()
            Dim crtableLogoninfos As New TableLogOnInfos
            Dim crtableLogoninfo As New TableLogOnInfo
            Dim CrTables As Tables
            Dim CrTable As Table

            'Create RPT name it "Order.rpt" using Crystal Report

            rptDocs1 = New Order
            rptDocs1.Load(Application.StartupPath + "\Order.rpt")

           'Replace the value of DatabaseName,ServerName,UserID,Password with your own SQLdatabase

            With connection
                connection.DatabaseName = "OnlineShop" 'myDataBase
                connection.ServerName = "Server\MSSQLSERVER" '127.0.0.1
                connection.UserID = "Admin" 'root
                connection.Password = "123456" '12345
            End With

            CrTables = rptDocs1.Database.Tables

            For Each CrTable In CrTables
                crtableLogoninfo = CrTable.LogOnInfo
                crtableLogoninfo.ConnectionInfo = connection
                CrTable.ApplyLogOnInfo(crtableLogoninfo)
            Next

            rptDocs1.SetDatabaseLogon(connection.UserID, connection.Password, connection.ServerName, connection.DatabaseName)
            rptDocs1.SetDataSource(SQL.SQLDS.Tables(0))
            ReportByOrder.CrystalReportViewer1.ReportSource = rptDocs1
            ReportByOrder.ShowDialog()
            ReportByOrder.Dispose()

            'ReportByOrder is the name of the report viewer form
            

        End If
 
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