Imports System.Data
Imports System.IO
Imports System.Data.DataTable
Imports MySql.Data.MySqlClient
Public Class frmReport
Public Shared data As New DataSet("Service")
Sub ShowData()
Dim sql As String
sql = "SELECT * FROM service ORDER BY Service_No"
Dim cmd As New MySqlCommand(sql, conn)
Dim adapter As New MySqlDataAdapter(cmd)
adapter.Fill(data, "Service")
DataGridView1.DataSource = data.Tables("Service")
With DataGridView1
.Columns(0).HeaderText = "ทดสอบ"
.Columns(1).HeaderText = "ทดสอบ2"
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'เคลี ยร์ ค่ าใน dtEx ออก
Dim ds_Ex As New Service
If ds_Ex._service.Rows.Count < 0 Then
ds_Ex._service.Rows.Clear()
End If
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim dr As DataGridViewRow
dr = DataGridView1.Rows(i)
ds_Ex._service.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value)
Next
Dim crv As New CrystalReport1
crv.SetDataSource(ds_Ex)
Dim frm As New Form1
frm.CrystalReportViewer1.ReportSource = crv
frm.ShowDialog()
End Sub
Private Sub frmReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call OpenDB()
ShowData()
End Sub
End Class
แก้ตอน select
sql = "SELECT isnull(service_date,'') as service_date,* FROM service ORDER BY Service_No"
Or
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim dr As DataGridViewRow
dr = DataGridView1.Rows(i)
ds_Ex._service.Rows.Add(iif(IsDBNull(dr.Cells(0).Value),"",dr.Cells(0).Value), iif(IsDBNull(dr.Cells(1).Value),"",dr.Cells(1).Value))
Next