how to load data from sql database onto an rdlc report with a DataSet Already

KwebenaAcquah-9104 306 Reputation points
2021-04-11T02:03:39.11+00:00

i am having a usercontrol that loads my rdlc report in c# like this;

 DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Subject1", typeof(int)));
            dt.Columns.Add(new DataColumn("Subject2", typeof(int)));
            dt.Columns.Add(new DataColumn("Subject3", typeof(int)));
            dt.Columns.Add(new DataColumn("Subject4", typeof(int)));
            DataRow dr = dt.NewRow();
            dr["Subject1"] = 23;
            dr["Subject2"] = 32;
            dr["Subject3"] = 33;
            dr["Subject4"] = 874;
            dt.Rows.Add(dr);
            ReportDataSource reportDataSource = new ReportDataSource();
            reportDataSource.Name = "DataSet1";
            reportDataSource.Value = dt;
            reportViewer.LocalReport.ReportPath = "C:\\Users\\hp\\source\\repos\\SMSKICSO\\SMSKICSO\\myReports\\TermlyReport.rdlc";
            reportViewer.LocalReport.DataSources.Add(reportDataSource);
            reportViewer.RefreshReport();

Problem: can i be able to doe this;
dr["Subject1"] = to a particular column in datagridview in wpf in c#;
such that when ever a particular row is selected that particular column row will be maped to my rdlc dataset1 report;
please can some one help resolve the issue thank you in advance;

Please: the sqlDatabase Data i have is thesame as the DataSet1;

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,262 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-04-11T04:15:51.377+00:00

    Hi,
    you can try following code:

             DataTable dt = new DataTable("DataSet1");
             string cnString = @"Data Source=SQLServer;Initial Catalog=Demo;Integrated Security=True";
             string sqlCmd = "SELECT Subject1, Subject2, Subject3, Subject4 FROM Tab1";
             using (da sqlDataAdap = new SqlDataAdapter(sqlCmd, cnString)) da.Fill(dt);
             ReportDataSource reportDataSource = new ReportDataSource() {Name = "DataSet1", Value = dt};
             reportViewer.LocalReport.ReportPath = "C:\\Users\\hp\\source\\repos\\SMSKICSO\\SMSKICSO\\myReports\\TermlyReport.rdlc";
             reportViewer.LocalReport.DataSources.Add(reportDataSource);
             reportViewer.RefreshReport();
    

1 additional answer

Sort by: Most helpful
  1. Bruno Markman 0 Reputation points
    2023-11-03T18:40:32.93+00:00

    Hi Peter, Yes I know the reportViewer is a reference to my report.

    But, I am not using the WinForm system, I am trying to use this feature with Blazor.

    On Blazor I do have not the possibility to drag-and-drop a component, but I am trying to figure out how to create and reportViewer object linked to my RDLC by code.

    Thanks

    0 comments No comments