简体   繁体   中英

How to add datasource to the local rdlc report dynamically?

I am new to to windows desktop application development.

I have one form containing the report viewer control. One .rdlc file that contains the design of the report.

My problem is that I want to bind the datasource for the report dynamically.

My Code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;


namespace WindowsFormsApplication3{

    public partial class Form1 : Form{ 

        OleDbConnection cn;
        OleDbCommand cmd;
        OleDbDataAdapter da;
        DataSet ds;

        public Form1(){
            InitializeComponent();
        } 

        private void Form1_Load(object sender, EventArgs e){
            cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Bill\\dbBill.accdb");
            string query = "Select * from BillMaster where BillNo=1";
            cn.Open();
            cmd = new OleDbCommand(query, cn);
            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);            
            cn.Close();         
        }   
    } 
 }

I want to bind the ds to my report.

Report file also contains the table and some text boxes that display the data.

How to do so.? and how to bind the data to the text box residing in .rdlc file? I have searched a lot but found the solutions of ASP.net only.

How to I achieve this in desktop application.

Please help.

Thanks in advance.

Please see this article :

void LocalReport_SubreportProcessing(
    object sender,
    Microsoft.Reporting.WebForms.SubreportProcessingEventArgs e)
{
    // get empID from the parameters
    int iEmpID = Convert.ToInt32(e.Parameters[0].Values[0]);

    // remove all previously attached Datasources, since we want to attach a
    // new one
    e.DataSources.Clear();

    // Retrieve employeeFamily list based on EmpID
    var employeeFamily = CpReportCustomData.Data.CustomDS.GetAllEmployeeFamily()
                         .FindAll(element => element.ID == iEmpID);

    // add retrieved dataset or you can call it list to data source
    e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource()
    {
        Name = "DSEmployeeFamily",
        Value = employeeFamily

    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM