jjob
jjob

Reputation: 11

Reportviewer not visible

I am using Visual Studio 2013 Professional and vb.net. When I start a new Windows Form and drag ReportViewer to the blank form, it does not appear on the form. At the bottom, it shows ReportViewer1; however, there is no reportviewer visible. This means I can't attach the viewer to a report or database.

If I start an entirely new project, I can drag the reportVierwer. This is a big project or I would just start a new one and do it.

Upvotes: 0

Views: 3043

Answers (2)

ba__friend
ba__friend

Reputation: 5913

For some reason (on VS2015) the ReportViewer instance is not added to the Controls collection of the form inside the InitializeComponents method.

Adding this line (reportViewer being the instance of ReportViewer) fixes the problem:

this.Controls.Add(reportViewer);

Upvotes: 2

PiotrWolkowski
PiotrWolkowski

Reputation: 8792

You can always add it in code behind manually, or to manually edit the designer.

Add the Load event handler to the form to refresh the report viewer on Load:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.ReportViewer1.RefreshReport()
End Sub

And in designer create the ReportViewer instance. In InitializeComponent:

Private Sub InitializeComponent()
    Me.ReportViewer1 = New Microsoft.Reporting.WinForms.ReportViewer()

    '...

    '
    'ReportViewer1
    '
    Me.ReportViewer1.Location = New System.Drawing.Point(13, 13)
    Me.ReportViewer1.Name = "ReportViewer1"
    Me.ReportViewer1.Size = New System.Drawing.Size(396, 246)
    Me.ReportViewer1.TabIndex = 0

    '...
End Sub
Friend WithEvents ReportViewer1 As Microsoft.Reporting.WinForms.ReportViewer

It's not ideal solution. When adding to your main code you are mixing presentation and logic. When modifying designer code there is a risk it will be overridden.

Perhaps some improvement would be to create yet another Form partial class where you put the above mentioned code. For instance, as LoadReportViewer method and then invoke the method from your code. This way you do not mingle designer code with logic and you don't risk the change is removed by designer.

Upvotes: 0

Related Questions