如何在datagridview中显示选定的行到crystal报表 [英] How to display selected rows in datagridview to crystal report

查看:58
本文介绍了如何在datagridview中显示选定的行到crystal报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用Crystal报表。我有一个商店程序,我想要做的是当我在datagridview中突出显示一行并单击报表按钮时,只有突出显示的行会被发送到报表查看器页面。我在这里尝试了一些解决方案,但没有运气。



报告按钮代码

This is my first time using Crystal reports.I have a store procedure and What i am trying to do is when i highlight a row in datagridview and click report button, that only the highlighted row gets sent to the report viewer page.I have tried some solutions here but no luck.

Report button code

Imports System.Data.SqlClient
Imports System.IO
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmView
    Dim cn As New SqlConnection("Data Source=.;Initial Catalog=DBSAS;Integrated Security=True")
    Dim cmd As New SqlCommand
    Dim da As New SqlDataAdapter
    Dim dt As New DataTable
    Dim i As Integer
    Dim a As New OpenFileDialog

    Private Sub btnRep_Click(sender As Object, e As EventArgs) Handles btnRep.Click 
            cn.Open()
        Dim report As New ReportDocument
        da.SelectCommand = New SqlCommand("EXEC usplatestDateEnrolled ", cn)
        report.RecordSelectionFormula = "{@studID}" & dgv1.SelectedRows(0).Cells(0).Value
        report.Load("C:\users\agent_edx44\My Documents\Visual studio 2012\projects\SASApp\Rep.rpt")
        frmReport.CrystalReportViewer1.ReportSource = report
        frmReport.CrystalReportViewer1.Refresh()
        frmReport.Show()
            cn.Close()
    End Sub

End Class





存储过程代码





Store procedure code

USE [DbSAS]
GO
/****** Object:  StoredProcedure [dbo].[uspLatestDateEnrolled]    Script Date: 02/07/2016 12:35:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[uspLatestDateEnrolled]
    -- Add the parameters for the stored procedure here

@studID INT = NULL


AS
BEGIN

    SET NOCOUNT ON;
SELECT sh.SchoolYear,
        sh.Levels,sh.Section, sh.DateEnrolled ,
        SI.StudentID,SI.Surname,SI.FirstName,SI.MiddleName, SI.StudAddress , 
        SI.BirthDay,SI.Gender, SI.Nationality, SI.BirthPlace,
        SI.TelNum,SI.SchoolWhereGraduated , 
        SI.DatesWhenGraduated, SI.SchoolLastAttended,
        SI.SchoolAddress, SI.Note,SI.StudImage,
        PI.Father_FirstName,PI.Father_LastName,
        PI.Father_MI,PI.Father_Occupation, 
        PI.Father_TelNUm, PI.Mother_FirstName, PI.Mother_LastName,
        PI.Mother_MI,PI.Mother_Occupation,PI.Mother_TelNum,
        PI.Contact_FirstName,PI.Contact_LastName,PI.Contact_MI,
        PI.Contact_Mobile,PI.Contact_TelNum,PI.Contact_Address  
        FROM StudentInformation SI 
        JOIN StudentHistory SH  
            ON SI.StudentID = SH.StudentID
        JOIN ParentInformation PI
        ON PI.ParentID = SI.ParentID
        WHERE si.StudentID = @studID
        ORDER BY DateEnrolled DESC

            SELECT * FROM StudentHistory WHERE StudentID = @studID
                ORDER BY DateEnrolled DESC
            SELECT TOP 1 SchoolYear,
                Levels,Section, DateEnrolled as LatestDate
                FROM StudentHistory
                WHERE studentID = @studID
                ORDER BY DateEnrolled DESC





我是什么尝试过:



当我运行此代码时,它表示





What I have tried:

When i run this code it says that

引用:

此行中的报告文件路径无效

Invalid report file path in this line




report.RecordSelectionFormula = "{@studID}" & dgv1.SelectedRows(0).Cells(0).Value



任何人都可以帮我修改我的代码。我被困在这里将近一个小时。在此先感谢


Can anyone help me to fix my code. I'm stuck here for almost an hour. Thanks in advance

推荐答案

第一个问题是您在不加载报告的情况下设置RecordSelectionFormula。

首先加载报告。

First problem is that you are setting the RecordSelectionFormula without loading the report.
Load the Report first.
Dim rptDoc as New ReportDocument
rptDoc.Load("path to my Report document")



删除SQL命令&数据适配器,因为它们不是必需的 - Crystal将为您处理连接数据库。



其次,您需要使用


Delete the SQL Command & Data Adapter as they are not required - Crystal will handle connecting to the database for you.

Secondly, you will need to use

report.SetParameterValue("paramName", paramValue)

填充Stored Procedure参数而不是RecordSelectionFormula



请注意:Crystal Reports不支持存储过程导致的多个表 - 请参阅;

Crystal Reports - 具有多个结果集的存储过程 [ ^ ]



亲切的问候

to populate the Stored Procedure parameter instead of RecordSelectionFormula

Please note: Crystal Reports does not support multiple tables as a result from a Stored Procedure - refer;
Crystal Reports - Stored Procedure with multiple result sets[^]

Kind Regards


这篇关于如何在datagridview中显示选定的行到crystal报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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