locked
How to bind datatable to reportviewer RRS feed

  • Question

  • User721740140 posted

    Hi,

    I want to bind datatable to reportviewer in which datatable is generated through code & columns are unknown.

    Tuesday, October 16, 2012 12:53 AM

All replies

  • User1495804863 posted

    Hi,

    Try this:

    ReportViewer1.Visible = true; 
    ReportViewer1.LocalReport.ReportPath = "report1.rdlc"; 
    var datasource = new ReportDataSource("report", dt); 
    ReportViewer1.LocalReport.DataSources.Clear(); 
    ReportViewer1.LocalReport.DataSources.Add(datasource); 
    ReportViewer1.LocalReport.Refresh(); 
    Tuesday, October 16, 2012 12:55 AM
  • User-179079940 posted

    Try this

    Use the below code to create DataTable

    protected DataTable Getdata(DateTime StartDate, DateTime EndDate)

    {

    try

    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());SqlCommand cmd = new SqlCommand("sprocname or your Query", conn);

    //Add your Parameters if you have any

    cmd.Parameters.AddWithValue("@StartDate", StartDate);

    cmd.Parameters.AddWithValue("@EndDate", EndDate);

    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);DataTable dt = new DataTable();

    da.Fill(dt);
    return dt;

    }
    catch (Exception ex)

    {
    throw ex;

    }

    }

     In your Main method follow the steps
    Trace.Write("Report RDLC: " + ReportViewer.LocalReport.ReportPath);

    ReportParameter[] Parameters = new ReportParameter[2];
    Parameters[0] = new ReportParameter("StartDate", Convert.ToString(StartDate));

    Parameters[1] = new ReportParameter("EndDate", Convert.ToString(EndDate));

    ReportViewer.LocalReport.SetParameters(Parameters);

    ReportViewer.LocalReport.DataSources.Clear();

    ReportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSourceName", GetData(StartDate, EndDate)));
    ReportViewer.LocalReport.Refresh();

    Tuesday, October 16, 2012 1:01 AM
  • User721740140 posted

    Not working because if i am assigning reportid column in dataset->datable & if i m passing datatable as

    "select reportid, reporttitle from reports"

    then only reportid is seen in report.

    what if datatable is generating column "empid,empname".

    I am unable to see any report in this scenario.

    Tuesday, October 16, 2012 1:36 AM