• 0
Sign in to follow this  
Paty007

Imprimir dados do DataGridView já carregado

Question

Muito boa noite, pessoal.

Estou precisando imprimir os dados que estão sendo exibidos em um DataGridView e não sei como usar o System.Drawing.Printing direito. Até assisti a algumas aulas, mas nada que suprisse a minha necessidade.

Alguém poderia me ajudar?

Estou usando o vb. Net 2012 ultimate e Access como bando de dados.

 

Desde já agradeço.

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Salve,

Precisa mesmo ser em .net? 

Não seria melhor jogar este conteúdo em uma div e depois chamar o método "Print()" do objeto "Window" em javascript ?

Exemplo:

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Impressao
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> content = new List<string>() { "estou com preguiça", "de pensar no momento" };
            GridView1.DataSource = content;
            GridView1.DataBind();
        }
    }
}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Impressao.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script   src="https://code.jquery.com/jquery-1.12.1.js"   integrity="sha256-VuhDpmsr9xiKwvTIHfYWCIQ84US9WqZsLfR4P7qF6O8="   crossorigin="anonymous"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="relatorio">
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>    
    </div>
    <input id="btnImprimir" type="button" value="Imprimir" />
    </form>
</body>
</html>
<script>
    $(document).ready(function () {
        $('#btnImprimir').on('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            window.open('Impressao.aspx', 'Impressao', null, true);
            return false;
        });
    });
</script>

Impressao.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Impressao.aspx.cs" Inherits="Impressao.Impressao" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script   src="https://code.jquery.com/jquery-1.12.1.js"   integrity="sha256-VuhDpmsr9xiKwvTIHfYWCIQ84US9WqZsLfR4P7qF6O8="   crossorigin="anonymous"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="conteudo_relatorio"></div>
    </form>
</body>
</html>
<script>
    $(document).ready(function () {
        if (window.opener != null) {
            var relatorio = window.opener.document.getElementById('relatorio');
            if (relatorio) {
                document.getElementById('conteudo_relatorio').innerHTML = relatorio.innerHTML;
                window.print();
            }
        }
    });
</script>

 

Share this post


Link to post
Share on other sites
  • 0

Oi Paty,

Agora que observei sua pergunta melhor...."DataGridView" (WindowsForms).... vou pensar em algo por aqui e posto depois.

 

[]s

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (var conn = new SqlConnection("Data Source=WELLINGTON-PC\\SQLEXPRESS; Initial Catalog=Northwind; Integrated Security=true;"))
            {
                var da = new SqlDataAdapter("SELECT CategoryId, CategoryName, Description FROM Categories ORDER BY CategoryName", conn);
                var ds = new DataSet();
                da.Fill(ds, "Categories");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = "Categories";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }
    }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this