question

KwebenaAcquah-9104 avatar image
0 Votes"
KwebenaAcquah-9104 asked KwebenaAcquah-9104 edited

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

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;

[3]: /answers/storage/attachments/95789-captureii.png

[4]: /answers/storage/attachments/96112-captutre.png


dotnet-csharpwindows-wpf
captufre.png (3.8 KiB)
captukre.png (7.1 KiB)
captureii.png (28.2 KiB)
captutre.png (23.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered KwebenaAcquah-9104 edited

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.


animation.gif (503.3 KiB)
1.png (18.7 KiB)
2.png (5.2 KiB)
3.png (7.9 KiB)
4.png (6.2 KiB)
5.png (17.2 KiB)
6.png (6.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.