how to import any/specific row from wpf datagridview onto a binded rdlc report dataset

KwebenaAcquah-9104 326 Reputation points
2021-05-10T00:33:59.2+00:00

i am having an rdlc report like this;

94980-captufre.png

and a datagridview like this in wpf c#;

95044-captukre.png

QUESTION
how can i get any selected row by the user from the datagridview to show in the above rdlc report according to the binded column as shown in the rdlc picture above;

for example: the value of columne Math will map to Rdlc report as maped from dataset1; English will also map [English] Name will also map [Name] in rdlc report can some one help me fix this (perharps it's from my button click event below code)

here is the code of the UserControl1 that loads my rdlc report in wpf

   DataTable dt = new DataTable("DataSet1");  
          string cnString = @"Data Source=SQLServer;Initial Catalog=Demo;Integrated Security=True";  
          string sqlCmd = "SELECT MATH, ENGLISH, SCIENCE 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();  

HERE is what i have tried with the button click event on wpf;

this is what i have tried via research onlin; please can some one try to comprehend and help me do the right thing please on this code below (my aim is to import only a specific row as will be selected by the user and load it in accordance to the dataset1 binded to the rdlc report with the columns on the row selected by the user) please i need help. (thanks)

private void btnprintResult_Click(object sender, RoutedEventArgs e)  
        {  
            DataTable dtRep = ((DataView)dgvRESULTPRINTER.SelectedItems).Table;  
            for (int i = 0; i < dgvRESULTPRINTER.SelectedItems.Count; i++)  
            {  
                for (int j = 1; j < 1; j++)  
                {  
                    dtRep.ImportRow(((DataRowView)dgvRESULTPRINTER.SelectedItems[i]).Row);  
                }  
            }  

my code keep importing all rows instead of only one selected row to show all rows are showing in my rdlc report;

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-05-10T06:59:16.92+00:00

    This is an extension of the previous thread.

    how to stop replication of row in rdlc report

    Please let me know where the answer in the previous thread does not meet your expectations, so that I can adjust the code.

    If the problem is that there are more columns in the data grid than in the report, and you want to extract some of the columns from the selected rows in the datagrid to fill in the report, you do not need to perform any operations.

    Did I misunderstand what you mean?

    Update:

    I have some doubts that we are using a different ReportViewer.

    To have a clearer understanding of how our operations are different, let me first talk about my complete steps.

    1. Add a nuget package Microsoft.ReportingServices.ReportViewerControl.Winforms.
    2. Right click on the project => Add => Add New Item => DataSet.
      96150-1.png
    3. Add a DataTable to the DataSet and then add the required three Columns.
      96140-2.png96261-3.png
    4. Like step 2, add a report wizard.
      96271-4.png
    5. Select the newly created DataSet in Data Source, and then click Next.
    6. Drag the column in Available fields to the Value column, click Next=>Next=> finish.
      96272-5.png96167-6.png
    7. Create a new window named TermlyReportShower, find WindowsFormsHost in the Toolbox and add a name "WindowsFormsHost". <WindowsFormsHost Name="windowsFormsHost" HorizontalAlignment="Left" Height="197" Margin="79,91,0,0" VerticalAlignment="Top" Width="339"/>
    8. Write the following code. MainWindow:
          public MainWindow()  
          {  
              InitializeComponent();  
              dataGrid.ItemsSource = GetDataTable().DefaultView;   
          }  
      
          private DataTable GetDataTable()   
          {  
              string connString = @"connString";  
              using (SqlConnection con = new SqlConnection(connString))  
              {  
                  SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM ScoreTable", con);  
                  DataTable dataTable = new DataTable();  
                  dataAdapter.Fill(dataTable);  
                  return dataTable;  
              }  
          }  
          private void button_Click(object sender, RoutedEventArgs e)  
          {  
              DataTable dtRep = ((DataView)dataGrid.ItemsSource).Table.Clone();  
              for (int i = 0; i < dataGrid.SelectedItems.Count; i++)  
              {  
                  dtRep.ImportRow(((DataRowView)dataGrid.SelectedItems[i]).Row);  
              }  
              dtRep.AcceptChanges();  
              TermlyReportShower window1 = new TermlyReportShower(dtRep);  
              window1.ShowDialog();  
          }  
      
      TermlyReportShower: public partial class TermlyReportShower : Window
      {
      public TermlyReportShower()
      {
      InitializeComponent();
      }
      public TermlyReportShower(DataTable dataTable)
      {
      InitializeComponent();
              ReportViewer reportViewer1 = new ReportViewer();  
      
              ReportDataSource reportDataSource = new ReportDataSource();  
              reportDataSource.Value = dataTable;  
              reportDataSource.Name = "DataSet1";  
              reportViewer1.LocalReport.ReportPath = @"D:\VsWorkSpace\......\Report1.rdlc";  
              reportViewer1.LocalReport.DataSources.Add(reportDataSource);  
              reportViewer1.RefreshReport();  
              windowsFormsHost.Child = reportViewer1;  
          }  
      }  
      

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.