如何为 MVC 的 ReportViewer 设置宽度

How to set width for ReportViewer for MVC

我正在使用 ReportViewer for MVC 在我的 MVC 应用程序的视图中显示报告。
如何设置此控件的宽度?

我已将宽度设置为 100%,但它不起作用,如该屏幕截图所示。

在我看来我使用:

<div>
    @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer, new { scrolling = "yes", width = "100%" })
</div>

而 aspx 页面是:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="margin: 0px; padding: 0px;width:100%;">
<form id="form1" runat="server">
<div  style="width:100%">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Assembly="ReportViewerForMvc"     Name="ReportViewerForMvc.Scripts.PostMessage.js" />
</Scripts>
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="100%"  Height="600px"></rsweb:ReportViewer>
</div>
</form>
</body>

尝试在您的控制器中编写以下代码

using System.Web.UI.WebControls;

  ReportViewer reportViewer = new ReportViewer();
  reportViewer.ProcessingMode = ProcessingMode.Local;
  reportViewer.SizeToReportContent = true;
  reportViewer.Width = Unit.Percentage(100);
  reportViewer.Height = Unit.Percentage(100);

注意:我正在使用来自 nuget 的 ReportViewerForMvc

我发现我需要从两个方面解决 width/height 问题 - 我添加了一个 CSS 来在加载时修改 ReportViewerWebForm.aspx 中的 iframe。

iframe {
  /*for the report viewer*/
  border: none;
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
}

除了我删除了

之外,其余的将与接受的答案相同

reportViewer.SizeToReportContent = True

来自我的控制器,因为它隐藏了我在相同 Report.cshtml 中呈现的更广泛报告所需的滚动条,即

@using ReportViewerForMvc; @{ ViewBag.Title = " Report"; }

<h2>Simple Report </h2>

@Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)

我的控制器:

 > 

 public ActionResult ReportByType()
        {
            var data =SomeFunction.CreateDataTable(GetReportDataFromDB());

            ReportViewer reportViewer = new ReportViewer();
            reportViewer.ProcessingMode = ProcessingMode.Local;

            reportViewer.LocalReport.ReportPath = 
              Request.MapPath(Request.ApplicationPath) + 
                   @"Views\Reports\ReportByType.rdlc";
            reportViewer.LocalReport.DataSources.Add(new 
               ReportDataSource("DataSet1", data));
           // reportViewer.SizeToReportContent = true; ---hides the scrollbar which i need
            reportViewer.Width = Unit.Percentage(100);
            reportViewer.Height = Unit.Percentage(100);

            ViewBag.ReportViewer = reportViewer;
            return PartialView("Report");

        }


        enter code here
@Html.ReportViewer(
     ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer,new { @width="100%"})