'Initialize the needed objects for report document.
Dim myOrderReport As New MyReport
Dim row As DataRow = Nothing
Dim row2 As DataRow = Nothing
Dim dataSet As New DataSet
Dim counter As Integer = ListView1.Items.Count
Dim tempCount As Integer = 0
Dim listViewItems As New ListViewItem()
'Create a new DataTable named Delivery Details
dataSet.Tables.Add("tbOrder")
'Create columns for the new DataTable named Delivery Details.
With dataSet.Tables(0).Columns
.Add("ลำดับ", Type.GetType("System.String"))
.Add("รหัสสินค้า", Type.GetType("System.String"))
.Add("ชื่อสินค้า", Type.GetType("System.String"))
.Add("ราคา", Type.GetType("System.Double"))
.Add("จำนวน", Type.GetType("System.Double"))
.Add("รวม", Type.GetType("System.Double"))
End With
'Create another DataTable called DeliveredItems
dataSet.Tables.Add("tbOrder")
'Create columns for the new DataTable named DeliveryItems.
With dataSet.Tables(1).Columns
.Add("ลำดับ", Type.GetType("System.String"))
.Add("รหัสสินค้า", Type.GetType("System.String"))
.Add("ชื่อสินค้า", Type.GetType("System.String"))
.Add("ราคา", Type.GetType("System.Double"))
.Add("จำนวน", Type.GetType("System.Double"))
.Add("รวม", Type.GetType("System.Double"))
End With
row = Nothing
row = dataSet.Tables(1).NewRow
'Store every data in an array for insertion.
Dim deliveredItemsCount As Integer = ListView1.Items.Count
Dim tempArray() As String
ReDim tempArray(5)
Dim deliveryListViewItem As New ListViewItem()
For counter = 0 To deliveredItemsCount
deliveryListViewItem = ListView1.Items.Item(counter)
row2(0) = ListView1.Items.Item(counter).ToString
row2(1) = deliveryListViewItem.SubItems(counter).ToString
row2(2) = deliveryListViewItem.SubItems(counter).ToString
row2(3) = deliveryListViewItem.SubItems(counter).ToString
row2(4) = deliveryListViewItem.SubItems(counter).ToString
'Insert new records to the DeliveredItems.
row2 = dataSet.Tables("tbOrder").NewRow
row2(0) = tempArray(0)
row2(1) = tempArray(1)
row2(2) = tempArray(2)
row2(3) = tempArray(3)
row2(4) = tempArray(4)
dataSet.Tables("DeliveredItems").Rows.Add(row2)
For counterClear = 0 To (deliveredItemsCount - 1)
tempArray(counter) = Nothing
Next counterClear
row2 = Nothing
Next counter
'Set Data Sources for the Crystal Report.
myOrderReport.SetDataSource(dataSet)
Form7.CrystalReportViewer1.ReportSource = myOrderReport
'Dispose after using.
dataSet.Dispose()
dataSet = Nothing
'REPORT OBJECT
Dim MyRpt As New MyReport
'DATASET, AND DATAROW OBJECTS NEEDED TO MAKE THE DATA SOURCE
Dim row As DataRow = Nothing
Dim DS As New DataSet
'ADD A TABLE TO THE DATASET
DS.Tables.Add("ListViewData")
'ADD THE COLUMNS TO THE TABLE
With DS.Tables(0).Columns
.Add("Field1", Type.GetType("System.String"))
.Add("Field2", Type.GetType("System.String"))
.Add("Field3", Type.GetType("System.String"))
End With
'LOOP THE LISTVIEW AND ADD A ROW TO THE TABLE FOR EACH LISTVIEWITEM
For Each LVI As ListViewItem In ListView1.Items
row = DS.Tables(0).NewRow
row(0) = LVI.SubItems(0).Text
row(1) = LVI.SubItems(1).Text
row(2) = LVI.SubItems(2).Text
DS.Tables(0).Rows.Add(row)
Next
'SET THE REPORT SOURCE TO THE DATABASE
MyRpt.SetDataSource(DS)
'ASSIGN THE REPORT TO THE CRVIEWER CONTROL
CRViewer.ReportSource = MyRpt
'DISPOSE OF THE DATASET
DS.Dispose()
DS = Nothing