Hallo Liebe C#ler,
ich drucke ein Datagridview, auf dem Druck ist über den Grid etwa 1/4 der Seite weiß, dann fängt erst das grid an, ich würde gerne oben anfangen zu drucken.
Vielen Dank im Vorraus für eure Hilfe
Hier mein Code
#region Drucken
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
//Set the left margin
int iLeftMargin = e.MarginBounds.Left;
//Set the top margin
int iTopMargin = e.MarginBounds.Top;
//Whether more pages have to print or not
bool bMorePagesToPrint = false;
int iTmpWidth = 0;
//For the first page to print set the cell width and header height
if (bFirstPage)
{
foreach (DataGridViewColumn GridCol in DgVmetallarten.Columns)
{
iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
(double)iTotalWidth * (double)iTotalWidth *
((double)e.MarginBounds.Width / (double)iTotalWidth))));
iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
// Save width and height of headres
arrColumnLefts.Add(iLeftMargin);
arrColumnWidths.Add(iTmpWidth);
iLeftMargin += iTmpWidth;
}
}
//Loop till all the grid rows not get printed
while (iRow <= DgVmetallarten.Rows.Count - 1)
{
DataGridViewRow GridRow = DgVmetallarten.Rows[iRow];
//Set the cell height
iCellHeight = GridRow.Height + 5;
int iCount = 0;
//Check whether the current page settings allo more rows to print
if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
bNewPage = true;
bFirstPage = false;
bMorePagesToPrint = true;
break;
}
else
{
if (bNewPage)
{
String strDate = DateTime.Now.ToString("dd.MM.yyyy");
//Datum drucken
e.Graphics.DrawString("Datum: " + strDate, new Font(DgVmetallarten.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width -
e.Graphics.MeasureString(strDate, new Font(DgVmetallarten.Font,
FontStyle.Bold), e.MarginBounds.Width).Width - 42), e.MarginBounds.Top -
e.Graphics.MeasureString("Customer Summary", new Font(new Font(DgVmetallarten.Font,
FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height + 25);
//Draw Columns
iTopMargin = e.MarginBounds.Height - 800;
foreach (DataGridViewColumn GridCol in DgVmetallarten.Columns)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight));
e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
new SolidBrush(GridCol.InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
(int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
iCount++;
}
bNewPage = false;
iTopMargin += iHeaderHeight;
}
iCount = 0;
//Draw Columns Contents
foreach (DataGridViewCell Cel in GridRow.Cells)
{
if (Cel.Value != null)
{
e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font,
new SolidBrush(Cel.InheritedStyle.ForeColor),
new RectangleF((int)arrColumnLefts[iCount], (float)iTopMargin,
(int)arrColumnWidths[iCount], (float)iCellHeight), strFormat);
}
//Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount],
iTopMargin, (int)arrColumnWidths[iCount], iCellHeight));
iCount++;
}
}
iRow++;
iTopMargin += iCellHeight;
}
//If more lines exist, print another page.
if (bMorePagesToPrint)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#region Member Variables
StringFormat strFormat; //Used to format the grid rows.
ArrayList arrColumnLefts = new ArrayList();//Used to save left coordinates of columns
ArrayList arrColumnWidths = new ArrayList();//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
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
int iHeaderHeight = 0; //Used for the header height
#endregion
#region Get Customer Details Button Click Event
/// <summary>
/// Handles the customer details button click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#endregion
#region Print Button Click Event
/// <summary>
/// Handles the print button click event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#endregion
#region Begin Print Event Handler
/// <summary>
/// Handles the begin print event of print document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
try
{
strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Near;
strFormat.LineAlignment = StringAlignment.Center;
strFormat.Trimming = StringTrimming.EllipsisCharacter;
arrColumnLefts.Clear();
arrColumnWidths.Clear();
iCellHeight = 0;
iRow = 0;
bFirstPage = true;
bNewPage = true;
// Calculating Total Widths
iTotalWidth = 0;
foreach (DataGridViewColumn dgvGridCol in DgVmetallarten.Columns)
{
iTotalWidth += dgvGridCol.Width;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
private void bt_gutOpen_Click(object sender, EventArgs e)
{
DatenInTempLadenGutschrift(1);
neueGutschrift newgut = new neueGutschrift();
newgut.Show();
}
private void bt_kundeDel_Click(object sender, EventArgs e)
{
}
#region Print Page Event
/// <summary>
/// Handles the print page event of print document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#endregion
#endregion
Schau dir die Koordinaten an, an die das alles gezeichnet wird. Da siehst du evtl. schnell was los ist, das geht durch anschauen des Codes nicht so schnell 😃
... vermutlich ist im Debugger anschauen gemeint, siehe [Artikel] Debugger: Wie verwende ich den von Visual Studio?.
Ohnehin ist es kein guter Still, eine Masse an Code zu posten und uns dann den Fehler suchen zu lassen, siehe [Hinweis] Wie poste ich richtig? Punkt 4.