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.
- Add a nuget package Microsoft.ReportingServices.ReportViewerControl.Winforms.
- Right click on the project => Add => Add New Item => DataSet.
- Add a DataTable to the DataSet and then add the required three Columns.
- Like step 2, add a report wizard.
- Select the newly created DataSet in Data Source, and then click Next.
- Drag the column in Available fields to the Value column, click Next=>Next=> finish.
- 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"/>
- Write the following code. MainWindow:
TermlyReportShower: public partial class TermlyReportShower : Windowpublic 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(); }
{
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.