Public Class Form1
Dim conn As New OleDb.OleDbConnection
Dim dta As New OleDb.OleDbDataAdapter
Dim sqlcom As New OleDb.OleDbCommand
Dim dts As New DataSet
Dim tcondetion As String
Dim strconn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\test.accdb"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
dts.Tables.Clear()
Dim sqlstr As String = "SELECT * FROM product"
dta = New OleDb.OleDbDataAdapter(sqlstr, conn)
dta.Fill(dts, "test")
dgvProduct.DataSource = dts.Tables("test")
dgvProduct.Columns(0).HeaderText = "pid"
dgvProduct.Columns(1).HeaderText = "pname"
dgvProduct.Columns(2).HeaderText = "price"
dgvProduct.Columns(3).HeaderText = "amount"
dgvProduct.Columns(4).HeaderText = "tatol"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
Dim dsEx As New DSProject
'เคลียร์ค่าใน dtEx ออก
If dsEx.dtEx.Rows.Count > 0 Then
dsEx.dtEx.Rows.Clear()
End If
'วน loop เพื่อเก็บค่าจาก DatagridView ใส่ใน DataTable dtEx
For i As Integer = 0 To dgvProduct.Rows.Count - 2
Dim dr As DataGridViewRow
dr = dgvProduct.Rows(i)
dsEx.dtEx.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value, dr.Cells(2).Value, dr.Cells(3).Value, dr.Cells(3).Value)
'ต้องตรงตามจำนวน column
Next
'แสดงรายงาน
Dim crv As New rptOrder
crv.SetDataSource(dsEx)
Dim frm As New Form2
frm.CrystalReportViewer1.ReportSource = crv
frm.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Public Class Form2
Dim r As New rptOrder
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource = r
End Sub
End Class