Create RDLC report dynamically at run-time from a DataGridView
<p>I have a form <code>AdvancedSearchForm</code> with a <strong>DataGridView</strong> control <code>dgrData</code> and a button <code>Report</code> in C# Winform. On click of the button <code>Report</code>, I wish that a form with a <strong>ReportView</strong> control be shown with the same columns as in the DataGridView with the same column Headers.</p>

<p><strong>Form with DataGridView and Button</strong></p>

<p><a href="https://i.stack.imgur.com/Q14zL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Q14zL.png" alt="enter image description here"></a></p>

<p><strong>Output expected on clicking Button “Report”:</strong></p>

<p><a href="https://i.stack.imgur.com/KdS75.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KdS75.png" alt="enter image description here"></a></p>

<p>My <strong>DatagridView</strong> (<code>dgrData</code>) Control is associated with</p>

<ol>
<li>SQL</li>
</ol>

<blockquote>
<pre><code>“Select Id, c_Name from Country”
</code></pre>
</blockquote>

<ol start="2">
<li>ConnectionString</li>
</ol>

<blockquote>
<pre><code>server=localhost;User Id=root;password=root;Persist Security Info=True;database=country_state
</code></pre>
</blockquote>

<p>To load data to the grid at runtime, I prepare the following <strong>DataAdapter</strong>:</p>

<pre><code>DataAdapter dataAdapter = DataAdapter.Current;
// I am passing the SQL statement and the table name to my database which knows the ConnectionString within the LoadData function

DataTable dt0 = dataAdapter.LoadData("select Id, c_Name from `country`", "country");
if (dt0 != null) {
   dgrData.DataSource = dt0;
}
</code></pre>

<p>Is it possible to call a child Form containing default <strong>reportviewer</strong> control, which shows report with a table containing columns corresponding to the datagridview (<code>dgrData</code>) along with data dynamically at runtime?</p>

<p><strong>Output expectation in detail:</strong></p>

<ol>
<li>On click of button, the reportviewer on the target form should get<br>
associated with the dataSource from the values in the
<strong>DataGridView</strong>. So, the <strong>ReportViewer</strong> control does not know anything about the data in the report till user clicks on the Report
Button at run-time.</li>
<li>I wish the the solution does not require creation of a separate RDLC
file, because It causes external dependency, to stop the current flow
and create a report file in a report file designer which can be
over-whelming to users.</li>
<li>I know nothing about RDLC designer and associating data source (I am
willing to learn (^_^), but I cannot force this learning requirement
on my team) and binding the data to the report. I would appreciate
working coding examples, if your help contains theory.</li>
<li>I know ReportViewer has been around for quite a long time now. Wish
that an example solution for 1-1 data mapping between data-grid and
ReportViewer was easier to find for someone in the future on SO.</li>
</ol>

<p><strong>Note:</strong> Please let me know if any additional data is required from my side in the comments. To show the current solution, I had to create and RDLC file where I had to put both the connection string and SQL at design time which I wish to avoid in the solution I am looking for. I wish to find a solution where the RDLC file is generated through some modular code that can be used on other solutions as well, rather than having to design it for each form where I have DataGrids.</p>