Hello Everyone,

how to print datagridview header text along with data.
I m able to print gridview values using printdocument.

Code is...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using PrintDataGridView;

namespace PrintDataGridView
{
    public partial class Form2 : Form
    {
        SqlConnection con;
        SqlDataAdapter ad;
        DataSet ds;
//DataGridViewPrint PrintGrid;

        PrintPreviewDialog pPreviewDialog;
         PrintDocument pDoc;

        public Form2()
        {
            InitializeComponent();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            pPreviewDialog.Show();
            pDoc.Print();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("initial catalog=duda;integrated
security=sspi");
            ds = new DataSet();
            ad = new SqlDataAdapter("select * from branch", con);
            ad.Fill(ds, "branch");






            pDoc = new PrintDocument();
            pDoc.PrintPage += new PrintPageEventHandler(pDoc_PrintPage);

            pPreviewDialog = new PrintPreviewDialog();
            pPreviewDialog.Document = pDoc;

          dGrid.DataSource = ds.Tables[0];
        }



        void pDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            int x = 0;
            int y = 0;
            int cell_height = 0;
            int colCount = dGrid.ColumnCount;
            int rowCount = dGrid.RowCount - 1;
            int current_col = 0;
            int current_row = 0;
            string value = "";

            Rectangle cell_border;
            SolidBrush brush = new SolidBrush(Color.Black);

            while (current_row < rowCount)
            {
                while (current_col < colCount)
                {
                    x += dGrid[current_col, current_row].Size.Width;
                    cell_height = dGrid[current_col,
current_row].Size.Height;
                    cell_border = new Rectangle(x, y, dGrid[current_col,
current_row].Size.Width, dGrid[current_col, current_row].Size.Height);
                    value = dGrid[current_col,
current_row].Value.ToString();
                    g.DrawRectangle(new Pen(Color.Black), cell_border);
                    g.DrawString(value, new Font("tahoma", 8), brush, x, y);
                    current_col++;
                }

                current_col = 0;
                current_row++;
                x = 0;
                y += cell_height;
            }

        }


Thanx..

-- 
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net

Reply via email to