How to display data in crystal report using 2 sql request and 2 datatables in dataset?

前端 未结 2 1665
误落风尘
误落风尘 2021-01-26 11:23

I have a dataset with 2 datatable aand I need to use 2 sql request to display data in crystal report. So I create 2 datatable in my dataset (DataTable1 and dataTable2) I tried t

2条回答
  •  粉色の甜心
    2021-01-26 11:49

    the solution is to implement a Datatable Method for each DataTable used in the Dataset: example for the 1st Datatable:

    protected DataTable DataTable1()
        {
            string sql = "MyRequest";
            SqlDataAdapter dad = new SqlDataAdapter(sql, con);
            DataSet1 ds = new DataSet1();
            dad.Fill(ds.Tables["NameOfDataTable"]);
            DataTable dt = ds.Tables["NameOfDataTable"];
            return dt;
    
        }
    

    and in the print button you add this code:

    try {
                DataSet ds = new DataSet();
                DataTable dt1 = DataTable1().Copy(); //the name of the method
                ds.Tables.Add(dt1);
                CrystalReport1 myreport = new CrystalReport1();
                myreport.SetDataSource(ds);
                crystalReportViewer1.ReportSource = myreport;
    
    }
    catch (Exception ex)
    {
       //code ...
    }
    

    It works successfully :)

提交回复
热议问题