using MultipleDataSources.Reports;
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DynamicDataSources
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath(@"Reports/RDL报表.rdlx")));
            rpt.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(locatedate);
            this.WebViewer1.Report = rpt;
        }
        private void locatedate(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
        {
            if (args.DataSourceName == "DataSource1")
            {
                if (args.DataSetName == "DataSet1")
                {
                    args.Data = returndata();
                }

            }
        }
        private DataTable returndata()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("产品编号");
            dt.Columns.Add("产品名称");
            dt.Columns.Add("单价");
            dt.Columns.Add("库存量");
            dt.Columns.Add("产地");
            dt.Columns.Add("产量");
            dt.Rows.Add("A001", "苹果", 10, 300, "中国", 2000);
            dt.Rows.Add("A002", "葡萄", 20, 200, "中国", 3000);
            dt.Rows.Add("A003", "香蕉", 30, 400, "中国", 2500);
            dt.Rows.Add("A004", "苹果", 10, 300, "中国", 1250);
            dt.Rows.Add("A005", "葡萄", 20, 200, "中国", 1360);
            dt.Rows.Add("A006", "香蕉", 30, 400, "中国", 1560);
            dt.Rows.Add("A007", "苹果", 10, 300, "中国", 5264);
            dt.Rows.Add("A008", "葡萄", 20, 200, "中国", 2465);
            dt.Rows.Add("A009", "香蕉", 30, 400, "中国", 3200);
            return dt;
        }
        private DataTable GetDataSource()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("产品编号");
            dt.Columns.Add("产品名称");
            dt.Columns.Add("单价");
            dt.Columns.Add("库存量");
            dt.Rows.Add("A10002", "苹果", 10, 50);
            dt.Rows.Add("A15681", "香蕉", 20, 150);
            dt.Rows.Add("A15682", "菠萝", 30, 550);
            dt.Rows.Add("A10102", "苹果", 40, 350);
            dt.Rows.Add("A12681", "香蕉", 50, 450);
            dt.Rows.Add("A15081", "菠萝", 60, 550);
            dt.Rows.Add("A10102", "苹果", 70, 650);
            dt.Rows.Add("A15281", "香蕉", 80, 250);
            dt.Rows.Add("A15671", "菠萝", 90, 650);
            return dt;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var a = this.DropDownList1.SelectedIndex;
            if (a == 0)
            {
                GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath(@"Reports/RDL报表.rdlx")));
                rpt.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(locatedate);
                this.WebViewer1.Report = rpt;
            }
            if (a == 1)
            {
                GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath(@"Reports/页面报表.rdlx")));
                rpt.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(locatedate);
                this.WebViewer1.Report = rpt;
            }
            if (a == 2)
            {
                GrapeCity.ActiveReports.SectionReport rpt = new GrapeCity.ActiveReports.SectionReport();
                rpt.LoadLayout(Server.MapPath(@"Reports\区域报表.rpx"));
                rpt.DataSource = GetDataSource();
                WebViewer1.Report = rpt;
            }
            if (a == 3)
            {
                SectionReport1 rpt = new SectionReport1();
                rpt.DataSource = GetDataSource();
                rpt.Run();
                this.WebViewer1.Report = rpt;
            }
        }
    }
}