protected void Page_Load(object sender, EventArgs e)
{
//aqui muestro mi reporte en la pagina
if (!Page.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ToString()))
{
SqlCommand cmd = new SqlCommand("SELECT * from tabla where Cod_user='" + Session["Cod_user"] + "'", con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable t = new DataTable();
da.Fill(t);
ReportDataSource datasource = new ReportDataSource("DataSet1", t); ReportViewer1.LocalReport.DataSources.Add(datasource);
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reportes/Report1.rdlc");
ReportViewer1.LocalReport.Refresh();
}
}
}
}
//boton imprimir
protected void Imprimir_Click(object sender, EventArgs e)
{
}
I have a report with reportviewer of an invoice that brings me data from the DB, I would like to be able to print it without the need to use the preview, that is, do it from programming with a separate button, that is, not use the one that has the reportviewer by default.
I hope you can help me thanks
SOLUTION WITH JQUEY
<!--script para imprimir-->
<script type="text/javascript">
function Print() {
var report = document.getElementById("<%=ReportViewer1.ClientID%>");
var div = report.getElementsByTagName("DIV");
var reportContents;
for (var i = 0; i < div.length; i++) {
if (div[i].id.indexOf("VisibleReportContent") !== -1) {
reportContents = div[i].innerHTML;
break;
}
}
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document :frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write(reportContents);
frameDoc.document.write("<style> @page { size: portrait; } .head { display: none; }></style>");
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
}
</script>
This is the solution given here , and here the solution proposed by Microsoft.
I hope you find it useful.