报告查看器多个数据集

时间:2015-04-27 10:02:42

标签: c# asp.net dataset reportviewer multiple-tables

我有一个应该显示报告查看器的Web应用程序。这是我的步骤:

1)将脚本管理器和报告查看器添加到新的Web表单

2)添加报告并将其绑定到名为dataset1

的数据集

3)为查询创建参数

4)为webform编写代码...

   private DataTable GetData(Int64 id_doc)
    {
        DataTable dt = new DataTable();
        string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringloginDb"].ConnectionString;
        try
        {
            using (var conn = new MySqlConnection(connStr))
            {
                string sSQL = "select * from details_doc where id_doc=@id_doc";
                MySqlCommand cmd = new MySqlCommand(sSQL, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add(new MySqlParameter("@id_doc", Session["id_doc"].ToString()));
                MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                adp.Fill(dt);
            }
        }
        catch (Exception)
        {
            throw;
        }
        return dt;
    }

    protected void showReport()
    {
        DataTable dt = GetData(Convert.ToInt64(Session["id_doc"].ToString()));
        rptViewer.LocalReport.Refresh();
        rptViewer.Reset();
        rptViewer.LocalReport.EnableExternalImages = true;
        this.rptViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
        ReportDataSource rds = new ReportDataSource("DataSet1", dt);
        rptViewer.LocalReport.DataSources.Add(rds);
        rptViewer.LocalReport.ReportPath = "ReportInvoice.rdlc";
        ReportParameter rptParam = new ReportParameter("ReportParameter1", Session["id_doc"].ToString());
        rptViewer.LocalReport.SetParameters(rptParam);
        rptViewer.LocalReport.Refresh();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack){
            if (Session["id_doc"] != null)
            {
                GetData(Convert.ToInt64(Session["id_doc"]));
                mostraReport();
            }
        }
    }

现在代码工作正常,但我需要报告显示另一个第二个表。我将添加另一个数据集和另一个报告参数,但后来我对代码感到困惑。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

I eventually managed to pass two parameters for my report viewer. Here's how:

  1. In design mode add a table adapter.
  2. Drag and drop the tables you need.
  3. Press use SQL Instruction, press Query generator and paste you query there. At "Where" condition specify this: WHERE (h.id_doc = @par1) AND (h.id_client= @par2)
  4. In report viewer add your two parameters
  5. Design your report.rdlc
  6. Write code-behind

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="actionBarStyle">@style/MyActionBarLogo</item> </style> <style name="MyActionBarLogo" parent="@style/Widget.AppCompat.Light.ActionBar"> <item name="background">@color/background_material_dark</item> <item name="logo">@drawable/icon</item> <item name="displayOptions">useLogo|showHome</item> </style>

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ap.brecht.myapplication" > <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

I hope this helps. Thanx.