In case when a
DataGridView[
^] component is not bind with some data source, a
DataSource property[
^] returns
Nothing
.
So, you need to create a data source for crystal report. See:
Dim dgv As DataGridView = Me.DataGridView
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(dgv.Columns.Cast(Of DataGridViewColumn)().Select(Function(x) New DataColumn(x.Name)).ToArray())
dt = dgv.Rows.Cast(Of DataGridViewRow)() _
.Select(Function(x) _
dt.LoadDataRow(New Object() _
{ _
x.Cells(0).Value, _
x.Cells(1).Value, _
x.Cells(2).Value _
}, False)) _
.CopyToDataTable()
Dim crystal As New CrystalReport1
crystal.SetDataSource(dt)
CrystalReportViewer1.ReportSource = crystal
CrystalReportViewer1.Refresh()
You can achieve the same by using simple
for ... next
loop by columns and rows in
DataGridView
.
Good luck!