Friday, March 23, 2012

Report Viewer Control in Local Mode ?

I run my reports in local mode and do something like this :

Code Snippet

//Local Processing mode for Reports

reportViewer.ProcessingMode = ProcessingMode.Local;

// Set RDL file based on handout/grid selected

if (gridRadioButton.Checked)

{

reportViewer.LocalReport.ReportPath = "Grid.rdlc";

}

else

{

//Handout

reportViewer.LocalReport.ReportPath = "Handout.rdlc";

}

//Refresh the report in order to show it to the user.

reportViewer.RefreshReport();

The problem is that both handout and grid is diiferent, Say User Selects the grid once it always shows the grid report. Even second time you change to handout still it shows the same Grid format and I debugged it and it picks the new report path but doesn't help? The application needs to be rerun to fix it.

Can anyone explain what is causing this and how to get rid of it.

Thanks,

Harsimrat

There are a couple of things you need to do:

1) I assume that the user can move back and forth between the reports at will? Not just change once? Therefore you should be checking to see if the user has actually *changed* before doing the appropriate reinitialization (IOW, do a comparison between current and new values)...

2) Reinitialization looks something like this:

Use the ReportViewer.Reset() method -- this is probably the bit you are missing Set the appropriate ReportPath, assuming these are not embedded resources, otherwise you want to set that Rebind the datasources -- this may or may not be required|||I tried adding Reset() and then it tells me cannot find the dataset. But without Reset() it runs fine. I think I need to rebind it somehow as my initial binding is done automatically. If you can please give an code example ?|||

Um, I did say you probably needed to re-bind... I think...

I would forget about the "automatically" bound altogether if you are switching between reports. But this is one of the reasons I told you to check to see whether they had actually *switched* reports -- because you don't want to do the reset and rebind if they have chosen the same report twice in a row. Does that make sense?

I will show you a code example in two parts. The first part shows you where I am actually refreshing the data, previous to changing the report. In my case the procedures for doing so are different depending on whether the source is a local XML file or from a data server --

I will pseudo code some parts of it that are not relevant to you... but this happens to be the reason why I need to switch reports in this particular form.... it doesn't matter that your reason is different, as it will still give you the idea of what you need to do, even in cases (like mine) that the datasets might be entirely different.

Code Snippet

If tsSourceIsServerInfo Then
Me.GetDataSetFromServer()
If [...] the last time they used a different report Then

Me.ReinitializeViewer(tThisReportName)
Else
Me.ReportViewer1.Visible = True ' it might not have been previously
End If
Else
Me.DataSet1.Reset()
Me.DataSet1.ReadXml(tsSource)
If [...] the last time they used a different report as above Then

Me.ReinitializeViewer(tThisReportName)
Else
Me.ReportViewer1.Visible = True
End If
End If

' carry on with initializing params here, then

Me.ReportViewer1.RefreshReport()

OK so far?

You see that the re-initialization is only done where necessary. Now for what happens inside the initialization:

Code Snippet

Private Sub ReinitializeViewer(ByVal tsReport As String)
Dim ReportDataSourceX = _

New Microsoft.Reporting.WinForms.ReportDataSource()
Me.ReportViewer1.Reset()
Me.ReportViewer1.LocalReport.ReportEmbeddedResource = tsReport

' change above line to

' Me.ReportViewer1.LocalReport.ReportPath = tsReport

' if not embedded

If tsReport is a certain report Then

ReportDataSourceX.Name = "Appropriate name for the data set for it"
ReportDataSourceX.Value = Me.BindingSourceDatabase
Else
ReportDataSourceX.Name = "Appropriate name here"
ReportDataSourceX.Value = Me.BindingSourceXML
End If


Me.ReportViewer1.LocalReport.DataSources.Add(ReportDataSourceX)
Me.ReportViewer1.Visible = True

End Sub

HTH,

>L<

No comments:

Post a Comment