none
ReportViewer Control on Form Programmatically... RRS feed

  • Question

  • Hi there everybody. :)

    So many articles around telling how to use this ReportViewer control, but none seems simply to get THE point ( or at least : MY point ).

    I use VB 2010.

    I have one simple Windows Form Project.

    I'd like to :

    1. Create a DataSet or DataTable by code.

    2. Set it as the datasource of the ReportViewer and simply make it show data.

    So the entire thing must be done locally and programmatically.

    The code ( one of the many I tried indeed ) I'm going with at the moment for my test is the following :

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'Test DataSet
            Dim dsTest As New DataSet("DataSetName")
            dsTest.Tables.Add("DataTableName")
            With dsTest.Tables("DataTableName")
                'PK
                .Columns.Add("ID")
                .PrimaryKey = New DataColumn() {.Columns("ID")}
                'Other Columns ...
                For i As Integer = 1 To 5
                    .Columns.Add("Column_" & i)
                Next
                '...
                'Rows
                Dim R As DataRow
                For i As Integer = 0 To 9
                    R = .NewRow
                    R(0) = i
                    For j As Integer = 1 To .Columns.Count - 1
                        R(j) = "Cell_" & i & j
                    Next
                    .Rows.Add(R)
                Next
            End With
    
            DGV.DataSource = dsTest.Tables("DataTableName")
    
            dsTest.WriteXmlSchema(Application.StartupPath & "\NomeReport.rdlc")
    
            Dim RDS As New Microsoft.Reporting.WinForms.ReportDataSource
            With RDS
                .Name = "NomeDataSet_NomeDataTable"
                .Value = dsTest.Tables("NomeDataTable")
            End With
            With ReportViewer1
                .ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
                .LocalReport.DataSources.Clear()
                .LocalReport.ReportPath = Application.StartupPath & "\NomeReport.rdlc"
                .LocalReport.DataSources.Add(RDS)
    
                .LocalReport.Refresh()
            End With
    
        End Sub
    
    End Class
    
    

     

    I have provided full code of the test Form, so everyone interested can test it in a bunch of seconds.

    DGV is a DataGridView, just to test generated data.

    Well, if someone knows simply what it takes, and how to do it, let me know.

    Thanks to everyone able to solve the problem.

     

     

     

    • Moved by Youen Zen Tuesday, January 31, 2012 7:25 AM To provide a better support (From:Visual Basic Language)
    Sunday, January 29, 2012 3:57 PM

All replies

  • Marco,

    Why do you write this line ?

     dsTest.WriteXmlSchema(Application.StartupPath & "\NomeReport.rdlc")
    
    

    The Schema Definition of your dataset is NOT the same thing as your local report. Please explain.

    Sunday, January 29, 2012 4:21 PM
  • Hi Sygrien, and thanks for attention.

    I put that code because of an "article" found on the web.

    Firstly I tried without any creation of a .rdlc file on disk, but it never worked.

    Then I found that post, but indeed that's not the right way.

    You're saying i should write the .rdlc locally ?

    But how to create it from scratch by code ?

    Or maybe I could make it without any .rdlc ? I don't know.

    What I only want is to get it work just by code, without any use of the designer ...

    Sunday, January 29, 2012 4:40 PM
  • The rdlc file contains the definition of your report. Which fields should be displayed and where, how to group data, when to start a new page, etc...

    I think this should be done through the designer.

    If you need to dump data without any formatting, then you probably don't need a report...

    Let me know the objective.

    Sunday, January 29, 2012 4:58 PM
  • Yes. That's the point.

    Let's think about a ReportViewer based Windows Forms Project that can actually "build" on the fly its own .rdlc file, assuming its structure from data.

    That is more or less what I was trying to do.

    You say "should be done through the designer", but I ask "maybe could be done by code" ?

    When I design a specific report I must know datasource schema, right ?

    But if I don't know the schema ? I only know I want my ReportViewer Control to show data from ANY schema.

    And that schema is passed by code...

    Sunday, January 29, 2012 5:14 PM
  • Hi macroGG,

    We have a Visual Studio Report Controls forum. In order to provide you a better support, I’ll move your thread from Visual Basic Language to Visual Studio Report Controls .

    Thank you for your understanding.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Tuesday, January 31, 2012 7:24 AM
  • Thank you Shanks. :)

    I've found myself a possible solution, and I can provide it if anyone is interested to know and to suggest...

    But there's really a lot of code, so I'm still looking for better ways...

     

    Tuesday, January 31, 2012 11:45 AM
  • Dear MarcoGG,

    Can you please elaborate your answer here,

    i am searching same solution, without designer, i want to populate the field on the reportviewer control.

    Please possible to post exambles.

    Thanks

    Tuesday, June 12, 2012 2:33 PM