Click here to Skip to main content
15,896,063 members

Datagrid view to crystal reports using declared dataset

Andrei Ramases asked:

Open original thread
i have this code that reflects datagrid values in crystal reports by using datasets. i commit error "The Report Has no Tables" thx in advance

VB
Dim report As New CrystalReport1
       Dim ds As New DataSet


       'Take the data and structure from the datagridview and return it as a dataset.  You can use
       '"Imports System.Data" declaration at the top of your project/class and remove the system.data
       'from the various parts of this function.

       Try
           'Add a new table to the dataset
           ds.Tables.Add("Main")

           'Add the columns
           Dim col As DataColumn

           'For each colum in the datagridveiw add a new column to your table
           For Each dgvCol As DataGridViewColumn In ProductsDataGridView.Columns
               col = New DataColumn(dgvCol.Name)
               ds.Tables("Main").Columns.Add(col)
           Next

           'Add the rows from the datagridview
           Dim row As DataRow
           Dim colcount As Integer = ProductsDataGridView.Columns.Count - 1

           For i As Integer = 0 To ProductsDataGridView.Rows.Count - 1
               row = ds.Tables("Main").Rows.Add

               For Each column As DataGridViewColumn In ProductsDataGridView.Columns
                   row.Item(column.Index) = ProductsDataGridView.Rows.Item(i).Cells(column.Index).Value
               Next

           Next


       Catch ex As Exception
           'Catch any potential errors and display them to the user
           MessageBox.Show("Error Converting from DataGridView" & ex.InnerException.ToString, _
           "Error Converting from DataGridView", MessageBoxButtons.OK, MessageBoxIcon.Error)

       End Try

       report.SetDataSource(ds.Tables(0))
       CrystalReportViewer1.ReportSource = report

       Form2.Show()
       Form2.DataGridView1.DataSource = ds.Tables(0)
       ds.Dispose()
       ds = Nothing
Tags: Visual Basic, Printing, DataGrid, Reporting

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900