*รูปนี้พอ crystal report preview ขึ้นมามันกลับกลาย เป็นขนาดแบบ Letter
Code Code (VB.NET)
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data.OleDb
Imports System.IO
Imports System.Data
Public Class FmrptSellGas
Friend _sellgassid As String = ""
Private Sub FmrptSellGas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Ico As New System.Drawing.Icon("1376639548_Sales-by-Payment-Method-rep.ico")
Me.Icon = Ico
Try
SellGass()
Me.WindowState = FormWindowState.Maximized
Catch ex As Exception
End Try
End Sub
Private Sub SellGass()
' select tbgoods.goodsname,tbcustomers.cs_name,tbsellgass.sellgassid,tbsellgass.goodsnum,tbsellgass.price,tbsellprice.sellprice,(tbsellgass.price*tbsellgass.goodsnum) as total from(((
'tbsellgass) left outer join tbgoods on tbsellgass.goodsid=tbgoods.goodsid) left outer join tbcustomers on tbsellgass.cs_id = tbcustomers.cs_id) left outer join tbsellprice on tbsellgass.sellgassid=tbsellprice.sellgassid where tbsellgass.sellgassid= '{?sellgassid}'
Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtAdapter As New OleDbDataAdapter
Dim objCmdCompany As New OleDbCommand
Dim dtAdapterCompany As New OleDbDataAdapter
Dim Conn As New OleDbConnection(ConnectDB.ConnectDB)
Conn.Open()
Dim ds As New DataSet
Dim strSQL As String
Dim dsCompany As New DataSet
Dim strSQLCompany As String
Dim dtsellgass, dtcompany As DataTable
' strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "select tbsellgass.sellgassid,tbgoods.goodsname,tbsellgass.goodsnum,tbsellgass.price,tbsellprice.sellprice,(tbsellgass.price*tbsellgass.goodsnum) as total,tbcustomers.cs_name,tbcustomers.cs_address,tbcustomers.cs_phone from((("
strSQL &= "tbsellgass) left outer join tbgoods on tbsellgass.goodsid=tbgoods.goodsid) left outer join tbcustomers on tbsellgass.cs_id = tbcustomers.cs_id) left outer join tbsellprice on tbsellgass.sellgassid=tbsellprice.sellgassid where tbsellgass.sellgassid= '" & Me._sellgassid & "' "
strSQLCompany = "select companyname,address,phone,fax from tbcompany"
' objConn.ConnectionString = strConnString
With objCmd
.Connection = Conn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
dtsellgass = ds.Tables(0)
With objCmdCompany
.Connection = Conn
.CommandText = strSQLCompany
.CommandType = CommandType.Text
End With
dtAdapterCompany.SelectCommand = objCmdCompany
dtAdapterCompany.Fill(dsCompany)
dtcompany = dsCompany.Tables(0)
'dtAdapter = Nothing
'objConn = Nothing
' Dim companynme As String
' companynme = dsCompany.Tables("tbcompany")(0).ToString
Dim rptSellGass As New ReportDocument
Dim directory As String = My.Application.Info.DirectoryPath
Dim companyname, address, phone, fax As String
Dim p As New PrintDialog
'rpt.Load(directory & "\myCrystalReport1.rpt")
'rptSellGass.Load("D:\SellGass\STOCK\STOCK\Reports\rptSellGas.rpt")
companyname = dtcompany.Rows(0)("companyname").ToString
address = dtcompany.Rows(0)("address").ToString
phone = dtcompany.Rows(0)("phone").ToString
fax = dtcompany.Rows(0)("fax").ToString
rptSellGass.Load(Application.StartupPath & "\rptSellGas.rpt")
' rptSellGass.Database.Tables(0).SetDataSource(dtsellgass)
' rptSellGass.OpenSubreport("rptCompany").SetDataSource(dsCompany.Tables("tbcompany"))
rptSellGass.SetDataSource(dtsellgass)
rptSellGass.SetParameterValue("companyname", companyname)
rptSellGass.SetParameterValue("address", address)
rptSellGass.SetParameterValue("phone", phone)
rptSellGass.SetParameterValue("fax", fax)
' rptSellGass.Subreports("rptCompany").Database.Tables(0).SetDataSource(dtcompany)
'rptSellGass.PrintOptions.PaperSize = PaperSize.DefaultPaperSize
'Dim objDefaultPrinter As New System.Drawing.Printing.PrinterSettings
'objDefaultPrinter.PrinterName = "SATO CG208"
Me.CrystalReportViewer1.ReportSource = rptSellGass
Me.CrystalReportViewer1.Refresh()
dtAdapter = Nothing
Conn.Close()
Conn = Nothing
End Sub
End Class
Dim JimFlag = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
Dim RAWkind As Integer
Dim dp As New System.Drawing.Printing.PrintDocument()
dp.PrinterSettings.PrinterName = "hp officeYet 2500 series" 'Network Printer \\ComputerName\PrinterName
For i As Integer = 0 To dp.PrinterSettings.PaperSizes.Count - 1
If dp.PrinterSettings.PaperSizes(i).PaperName = "A9 ทั้งใหญ่ทั้งยาว" Then
RAWkind = CInt(dp.PrinterSettings.PaperSizes(i).GetType().GetField("kind", JimFlag).GetValue(dp.PrinterSettings.PaperSizes(i)))
Exit For
End If
Next
rptSellGass.PrintOptions.PaperSize = CType(RAWkind, CrystalDecisions.Shared.PaperSize)
Me.CrystalReportViewer1.ReportSource = rptSellGass
ถ้า Crystal Reports คือคำตอบของคุณ
ผม Guide ให้คุณเขียน User Define Function บน Crystal Reports ถ้านึกภาพไม่ออก
มันก็คล้ายฯ กับการเขียน Store Procedure บน MySQL หรือ SQL Server นั่นแหละครับ