如何将数据网格视图打印到水晶报告? [英] How do I print a datagridview to crystal report?

查看:84
本文介绍了如何将数据网格视图打印到水晶报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的datagridview(dgvCart)的数据是由文本框中的值填充的,它没有绑定在数据库中。



我需要为我创建一个数据集吗?数据表..?数据表的值将来自datagridview(dgvCart)。所以我可以将它打印到水晶报告中。



或者dgvCart可以直接打印在水晶报告中吗?



i在模块上创建了以下代码..

i不知道它是否正确。



我的尝试:



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:

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

推荐答案

您不在CR中打印DGV。您可以使用所需的布局在CR中创建报表(使用报表中的表格)并将数据源设置为与DGV相同的数据。然后打印报告,而不是DGV。
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.


'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


这篇关于如何将数据网格视图打印到水晶报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆