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 students"
dta = New OleDb.OleDbDataAdapter(sqlstr, conn)
dta.Fill(dts, "STD_DATA")
DataGridView1.DataSource = dts.Tables("STD_DATA")
DataGridView1.Columns(0).HeaderText = "student ID"
DataGridView1.Columns(1).HeaderText = "Student Name"
DataGridView1.Columns(2).HeaderText = "Year"
DataGridView1.Columns(3).HeaderText = "Mobile"
End Sub
และส่วนน๊้คึอ ปุ่มคริก เพื่อ report
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable
dt = Me.DataGridView1.DataSource
Dim cr As New CrystalReport1
cr.SetDataSource(dt)
Dim R As New Form2
R.CrystalReportViewer1.ReportSource = cr
R.ShowDialog()
End Sub
Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReport.Click
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString, strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "SELECT * FROM audit WHERE CustomerID = '" & Me.txtCustomerID.Text & "' "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds, "myDataTable")
dt = ds.Tables(0)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Dim rpt As New ReportDocument()
Dim directory As String = My.Application.Info.DirectoryPath
'rpt.Load(directory & "\myCrystalReport1.rpt")
rpt.Load("C:\DemoCrystalReport2\DemoCrystalReport2\myCrystalReport1.rpt")
rpt.SetDataSource(dt)
Me.CrystalReportViewer1.ReportSource = rpt
Me.CrystalReportViewer1.Refresh()
End Sub