代码之家  ›  专栏  ›  技术社区  ›  Maestro1024

如何以编程方式为ASP.NET ReportViewer控件设置数据源?

  •  5
  • Maestro1024  · 技术社区  · 14 年前

    如何以编程方式设置ASP.NET ReportViewer控件的数据源?

    我有一个vs 2008 ReportViewer控件,希望在两个不同的报表之间切换。

    我可以通过设置报表源和刷新控件来切换报表,但我看不到在哪里设置数据源。

    每个报表都有自己的数据源,如果在构建控件时对它们进行初始配置,这是可以的,但我需要在它们之间进行切换。

    3 回复  |  直到 14 年前
        1
  •  4
  •   Alex Shirshov    14 年前

    我假设问题是关于ReportViewer控件。

    reportViewer.LocalReport.DataSources.Clear();
    reportViewer.LocalReport.DataSources.Add(new ReportDataSource("dsname", source));
    

    "dsname" 是数据源的名称,您可以找到它的.rdlc文件。 source 是包含要在报表中显示的数据的变量。

        2
  •  1
  •   Brett    11 年前

    1)基本标记:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <rsweb:ReportViewer ID="rptView" Width="1000px" ProcessingMode="Local"
            Font-Names="Verdana" Font-Size="8pt" InteractiveDeviceInfos="(Collection)" 
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" runat="server"  >
    </rsweb:ReportViewer>
    

    2)编辑报表XML。设置数据集字段名:

    <DataSets>
      <DataSet Name="dsSource">
        <Fields>
          <Field Name="MyField1">
            <DataField>MyField1</DataField>
            <rd:TypeName>System.String</rd:TypeName>
          </Field>
        <Query>
          <DataSourceName>dsSource</DataSourceName>
          <CommandText>/* Local Query */</CommandText>
        </Query>
      </DataSet>
    </DataSets>
    

    3)在两个报表查看器上设置数据源(不知道为什么…两者都是必需的)

    SqlConnection cn = new SqlConnection(_connectionString);
    SqlCommand cmd = new SqlCommand("dbo.MyProc", cn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable tbl = new DataTable();
    
    cn.Open();
    da.Fill(tbl);
    cn.Close();
    
    rptView.Visible = true;
    rptView.LocalReport.DataSources.Clear();
    ReportDataSource rptData = new ReportDataSource("dsSource", tbl);
    
    LocalReport r = new LocalReport();
    r.ReportPath = Server.MapPath("~/Reports/MyReport.rdlc");
    r.DataSources.Add(rptData);
    
    rptView.LocalReport.DataSources.Add(rptData);
    rptView.LocalReport.ReportPath = Server.MapPath("~/Reports/MyReport.rdlc");
    rptView.LocalReport.Refresh();
    
        3
  •  0
  •   Ron Chong    10 年前
    GlobalReportViewer.AsyncRendering = True
        If Session.Count > 0 Then
            For i As Integer = 0 To Session.Count - 1
                If Session(i).GetType().ToString() = "Microsoft.Reporting.WebForms.ReportHierarchy" Then
                    Session.RemoveAt(i)
                End If
            Next
        End If
        GlobalReportViewer.LocalReport.ReportPath = ""
    GlobalReportViewer.LocalReport.ReleaseSandboxAppDomain()
    GlobalReportViewer.LocalReport.DataSources.Add(New ReportDataSource("XXXDataSet_YYYTable", "ObjectDSZZZ"))
    GlobalReportViewer.LocalReport.ReportPath = "Reports\AAAReport.rdlc"
    GlobalReportViewer.LocalReport.Refresh()