Inhalt von dataGridView1 drucken

julmer39

Grünschnabel
Hallo zusammen,

ich möchte gerne den Inhalt einer Tabelle ausdrucken lassen. Geht auch alles soweit es kommt nur eine leere Seite aus dem Drucker. Hat von euch so etwas schon mal gehabt?

Viele Grüße

Jochen

Code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;

namespace WindowsFormsApplication1
{
publicpartialclassForm1 : Form
{
privateDataGridView dataGridView1 = newDataGridView();
privateBindingSource bindingSource1 = newBindingSource();
privateSqlDataAdapter dataAdapter = newSqlDataAdapter();
privateButton reloadButton = newButton();
privateButton closeButton = newButton();
privateButton printButton = newButton();
privatePrintDocument printDocument1 = newPrintDocument();

privateStringFormat strFormat = newStringFormat();
ArrayList arrColumnLefts = newArrayList();//Used to save left coordinates of columns
ArrayList arrColumnWidths = newArrayList();//Used to save column widths
int iCellHeight = 0; //Used to get/set the datagridview cell height
int iTotalWidth = 0;
int iRow = 0;//Used as counter
int iHeaderHeight = 0; //Used for the header height
int iCount = 0;
bool bFirstPage = false; //Used to check whether we are printing first page
bool bNewPage = false;// Used to check whether we are printing a new page

[STAThreadAttribute()]
publicstaticvoid Main()
{
Application.Run(newForm1());
}
public Form1()
{
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.BackgroundColor = Color.LightSkyBlue;

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;
dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;

dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black;
dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black;

//Erste Spalte ausblenden
dataGridView1.RowHeadersVisible = false;

this.dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Transparent;

reloadButton.Text = "reload";
closeButton.Text = "close";
printButton.Text = "print";

reloadButton.Click += new System.EventHandler(buttonreload_Click);
closeButton.Click += new System.EventHandler(buttonclose_Click);
printButton.Click += new System.EventHandler(buttonprint_Click);

FlowLayoutPanel panel = newFlowLayoutPanel();
panel.Dock = DockStyle.Top;
panel.AutoSize = true;
panel.Controls.AddRange(newControl[] { reloadButton,});
panel.Controls.AddRange(newControl[] { closeButton, });
panel.Controls.AddRange(newControl[] { printButton, });

this.Controls.AddRange(newControl[] { dataGridView1, panel });
this.Load += new System.EventHandler(Form1_Load_1);
this.Text = "ITP Datenbankverbindung";
}
privatevoid Form1_Load_1(object sender, System.EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("SELECT Device_Types.TypeName, Device_Types.ArticleNumber, Protocol_Results.Tester, Protocol_Results.Workstation, Protocol_Results.CreationDate, Device_Devices.SerialNumber FROM Device_Types INNER JOIN Device_Devices ON Device_Types.TypeID = Device_Devices.TypeID INNER JOIN Protocol_Results ON Device_Devices.DeviceID = Protocol_Results.DeviceID");
}
privatevoid buttonreload_Click(object sender, System.EventArgs e)
{
GetData(dataAdapter.SelectCommand.CommandText);
}
privatevoid buttonclose_Click(object sender, System.EventArgs e)
{
Close();
}
privatevoid buttonprint_Click(object sender, System.EventArgs e)
{
printReciept();
}
privatevoid printReciept()
{
//Open the print dialog
PrintDialog printDialog = newPrintDialog();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true;
//Get the document
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Test Page Print";
printDocument1.Print();
}
}
privatevoid GetData(string selectCommand)
{
try
{
String connectionString =
"Data Source=CZC41649HM\\SQLEXPRESS;" +
//"Trusted_Connection=yes;" +
"Integrated Security=True;" +
//"database=ITPLocal; " +
"Initial Catalog=ITPLocal;" +
"connection timeout=30";

dataAdapter = newSqlDataAdapter(selectCommand, connectionString);

SqlCommandBuilder commandBuilder = newSqlCommandBuilder(dataAdapter);

DataTable table = newDataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
}
privatevoid dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
}
privatevoid printDocument1_BeginPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
try
{
strFormat = newStringFormat();
strFormat.Alignment = StringAlignment.Near;
strFormat.LineAlignment = StringAlignment.Center;
strFormat.Trimming = StringTrimming.EllipsisCharacter;
arrColumnLefts.Clear();
arrColumnWidths.Clear();
iCellHeight = 0;
iCount = 0;
bFirstPage = true;
bNewPage = true;
// Calculating Total Widths
iTotalWidth = 0;
foreach (DataGridViewColumn dgvGridCol in dataGridView1.Columns)
{
iTotalWidth += dgvGridCol.Width;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
 

Neue Beiträge

Zurück