Monday, February 21, 2011

Using MicrosoftReportViewer with Web Forms programmatically

Hi, I'm repeating my question because the prior one under this topic was lost. Could you please help me? I want to use MicrosoftReportViewer for Web Forms so that the DataSource be set programmatically. There is some sample code on the Internet for Windows Forms but I haven't found anything for Web Forms. For example, here is some code I've tried to use. It gives no errors but nothing is displayed.

How should I modify the code to display a table in the ReportViewer?

Imports System.Data Imports Microsoft.Reporting.WebForms

Partial Class TestReportViewer Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    CreateReport()
End Sub

Sub CreateReport()
    Dim dt As DataTable
    Dim rpt As ReportDataSource
    dt = New DataTable("Sample")
    With dt
        .Columns.Add("No", GetType(Integer))
        .Columns.Add("Name")
        .Rows.Add(1, "A1")
        .Rows.Add(2, "A2")
        .Rows.Add(3, "A3")
        .Rows.Add(4, "A4")
        .AcceptChanges()
    End With
    rpt = New ReportDataSource
    rpt.DataMember = "Sample"
    rpt.Value = dt
    rpt.Name = "test"

    With ReportViewer1
        .LocalReport.DataSources.Add(rpt)
        .DataBind()
        .LocalReport.Refresh()
    End With
End Sub

End Class

From stackoverflow
  • Have a look at this link: http://odetocode.com/articles/128.aspx

    You can basically do the same as in WinForms:

    Instantiate ReportViewer in your code behind and set

     ReportViewer reportViewer1 = new ReportViewer(); 
     reportViewer1.ServerUrl="http://localhost/ReportServer";
     reportViewer1.ReportPath="/SampleReports/Sales Order Detail";
    

    Or

    reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("datasetname", dataSet.Tables[0]));
    

    Michael

0 comments:

Post a Comment