About printing the DataGrid

Source: Internet
Author: User
Tags bool comparison table constructor count rowcount tostring
datagrid| print using System;
Using System.Windows.Forms;
Using System.Drawing;
Using System.Drawing.Printing;
Using System.Data;
Using System.Collections;

Namespace Sx_mdi
{
<summary>
Summary description for Datagridprinter.
</summary>
public class Datagridprinter
{

Private PrintDocument Theprintdocument;
Private DataTable thetable;
Private DataGrid Thedatagrid;

public int rowcount = 0; Current count of rows;
Private Const int kverticalcellleeway = 10;
public int pagenumber = 1;

int pagewidth;
int pageheight;
int TopMargin;
int bottommargin;

public string Receieve_amount; Quantity of goods received
public string Receieve_moneys; Receipt Amount
public string Send_amount; Number of shipments
public string Send_moneys; Shipping Amount


Public Datagridprinter (DataGrid Agrid, PrintDocument aprintdocument, DataTable aTable)
{
//
Todo:add constructor Logic here
//
Thedatagrid = Agrid;
Theprintdocument = aprintdocument;
thetable = aTable;


Get Print parameters
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;

}

public void GetValues (string sh_amount,string sh_moneys,string fh_amount,string Fh_moneys)
{
//
Todo:add constructor Logic here
//
Receieve_amount = Sh_amount;
Receieve_moneys = Sh_moneys;
Send_amount = Fh_amount;
Send_moneys = Fh_moneys;


}

public void Drawheader (Graphics g)
{
SolidBrush Forebrush = new SolidBrush (Thedatagrid.headerforecolor);
SolidBrush Backbrush = new SolidBrush (Thedatagrid.headerbackcolor);
Pen Thelinepen = new Pen (thedatagrid.gridlinecolor, 1);
StringFormat CellFormat = new StringFormat ();
CellFormat. trimming = Stringtrimming.ellipsischaracter;
CellFormat. FormatFlags = Stringformatflags.nowrap | Stringformatflags.linelimit;


g.DrawString ("Send and receive comparison table", New Font ("Arial", FontStyle.Bold), New SolidBrush (Thedatagrid.headerbackcolor), PAGEWIDTH/2 , TopMargin, New StringFormat ());

int columnwidth = pagewidth/thetable.columns.count-20;


int initialrowcount = ROWCOUNT;

Draw the table header
float startxposition = thedatagrid.location.x;
RectangleF nextcellbounds = new RectangleF (0,0, 0, 0);

RectangleF headerbounds = new RectangleF (0, 0, 0, 0);

Headerbounds.x = thedatagrid.location.x;
Headerbounds.y = thedatagrid.location.y + TopMargin + (rowcount-initialrowcount) * (TheDataGrid.Font.SizeInPoints + kver Ticalcellleeway);
Headerbounds.height = TheDataGrid.Font.SizeInPoints + Kverticalcellleeway;
Headerbounds.width = PageWidth;

G.fillrectangle (Backbrush, headerbounds);

for (int k = 0; k < TheTable.Columns.Count; k++)
{

String nextcolumn = Thetable.columns[k]. ToString ();
Switch (nextcolumn)
{
Case "Code":
Nextcolumn = "Receiving the waybill number";
Break
Case "Bp_code":
Nextcolumn = "Clothing number";
Break
Case "Bp_name":
Nextcolumn = "Clothing name";
Break
Case "Receieve_prices":
Nextcolumn = "Receiving price";
Break
Case "Receieve_amounts":
Nextcolumn = "Receipt Quantity";
Break
Case "Receieve_moneys":
Nextcolumn = "Receiving Amount";
Break
Case "Send_amunts":
Nextcolumn = "Number of shipments";
Break
Case "Send_prices":
Nextcolumn = "Shipping price [yuan/dozen]";
Break
Case "Send_moneys":
Nextcolumn = "Shipping Amount";
Break
Default
Break
}

RectangleF cellbounds = new RectangleF (startxposition, THEDATAGRID.LOCATION.Y + TopMargin + (rowcount-initialrowcount) * (TheDataGrid.Font.SizeInPoints + Kverticalcellleeway),
ColumnWidth
TheDataGrid.HeaderFont.SizeInPoints + Kverticalcellleeway);
Nextcellbounds = Cellbounds;

if (startxposition + columnwidth <= pagewidth)
{

g.DrawString (Nextcolumn, Thedatagrid.headerfont, Forebrush, Cellbounds, CellFormat);
}

Startxposition = startxposition + columnwidth;


}

if (Thedatagrid.gridlinestyle!= datagridlinestyle.none)
G.drawline (Thelinepen, thedatagrid.location.x, Nextcellbounds. Bottom, PageWidth, Nextcellbounds. Bottom);
}

public bool Drawrows (Graphics g)
{
int lastrowbottom = TopMargin;

Try
{
SolidBrush Forebrush = new SolidBrush (Thedatagrid.forecolor);
SolidBrush Backbrush = new SolidBrush (Thedatagrid.backcolor);
SolidBrush Alternatingbackbrush = new SolidBrush (Thedatagrid.alternatingbackcolor);
Pen Thelinepen = new Pen (thedatagrid.gridlinecolor, 1);
StringFormat CellFormat = new StringFormat ();
CellFormat. trimming = Stringtrimming.ellipsischaracter;
CellFormat. FormatFlags = Stringformatflags.nowrap | Stringformatflags.linelimit;
int columnwidth = pagewidth/thetable.columns.count-20;

int initialrowcount = ROWCOUNT;

RectangleF rowbounds = new RectangleF (0, 0, 0, 0);

Draw vertical Lines

ArrayList Lines = new ArrayList ();

Draw the table


for (int i = Initialrowcount i < TheTable.Rows.Count; i++)
{

DataRow dr = Thetable.rows[i];
int startxposition = thedatagrid.location.x;

Rowbounds.x = thedatagrid.location.x;
Rowbounds.y = thedatagrid.location.y + TopMargin + ((rowcount-initialrowcount) +1) * (TheDataGrid.Font.SizeInPoints + kve Rticalcellleeway);
Rowbounds.height = TheDataGrid.Font.SizeInPoints + Kverticalcellleeway;
Rowbounds.width = PageWidth;
Lines.add (Rowbounds.bottom);

if (i%2 = 0)
{
G.fillrectangle (Backbrush, rowbounds);
}
Else
{
G.fillrectangle (Alternatingbackbrush, rowbounds);
}


for (int j = 0; J < TheTable.Columns.Count; J + +)
{



RectangleF cellbounds = new RectangleF (startxposition,
THEDATAGRID.LOCATION.Y + TopMargin + ((rowcount-initialrowcount) + 1) * (TheDataGrid.Font.SizeInPoints + Kverticalcellle eWAY),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);


if (startxposition + columnwidth <= pagewidth)
{
g.DrawString (Dr[j]. ToString (), Thedatagrid.font, Forebrush, Cellbounds, CellFormat);
Lastrowbottom = (int) cellbounds. Bottom;
}

Startxposition = startxposition + columnwidth;
}


rowcount++;

if (ROWCOUNT * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway) > (PageHeight * pagenumber)-(BOTTOMMARGIN+TOPMA Rgin))
{
Drawhorizontallines (g, Lines);
Drawverticalgridlines (g, Thelinepen, ColumnWidth, Lastrowbottom);
return true;
}


}

Drawhorizontallines (g, Lines);
Drawverticalgridlines (g, Thelinepen, ColumnWidth, Lastrowbottom);


RectangleF cellbound1 = new RectangleF (thedatagrid.location.x,
THEDATAGRID.LOCATION.Y + TopMargin + (rowcount + 1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);

g.DrawString ("Total:", Thedatagrid.font, Forebrush, Cellbound1, New StringFormat ());


RectangleF cellbound2 = new RectangleF (Thedatagrid.location.x+columnwidth,
THEDATAGRID.LOCATION.Y + TopMargin + (rowcount + 1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);

g.DrawString (This.receieve_amount. ToString (), Thedatagrid.font, Forebrush, Cellbound2, New StringFormat ());


RectangleF Cellbound3 = new RectangleF (Thedatagrid.location.x+2*columnwidth,
THEDATAGRID.LOCATION.Y + TopMargin + (rowcount + 1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);

g.DrawString (This.receieve_moneys. ToString (), Thedatagrid.font, Forebrush, Cellbound3, New StringFormat ());

RectangleF Cellbound4 = new RectangleF (Thedatagrid.location.x+3*columnwidth,
THEDATAGRID.LOCATION.Y + TopMargin + (rowcount + 1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);

g.DrawString (This.send_amount. ToString (), Thedatagrid.font, Forebrush, Cellbound4, New StringFormat ());

RectangleF Cellbound5 = new RectangleF (Thedatagrid.location.x+4*columnwidth,
THEDATAGRID.LOCATION.Y + TopMargin + (rowcount + 1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);

g.DrawString (This.send_moneys. ToString (), Thedatagrid.font, Forebrush, Cellbound5, New StringFormat ());


g.DrawString ("Total:", Thedatagrid.headerfont, Forebrush, Cellbounds, CellFormat);
return false;

}
catch (Exception ex)
{
MessageBox.Show (ex. Message.tostring ());
return false;
}

}

void Drawhorizontallines (Graphics g, ArrayList Lines)
{
Pen Thelinepen = new Pen (thedatagrid.gridlinecolor, 1);

if (Thedatagrid.gridlinestyle = = Datagridlinestyle.none)
Return

for (int i = 0; i < lines. Count; i++)
{
G.drawline (Thelinepen, thedatagrid.location.x, (float) lines[i], PageWidth, (float) lines[i]);
}
}

void Drawverticalgridlines (Graphics g, Pen thelinepen, int columnwidth, int bottom)
{
if (Thedatagrid.gridlinestyle = = Datagridlinestyle.none)
Return

for (int k = 0; k < TheTable.Columns.Count; k++)
{
G.drawline (Thelinepen, thedatagrid.location.x + k*columnwidth,
THEDATAGRID.LOCATION.Y + TopMargin,
Thedatagrid.location.x + k*columnwidth,
bottom);
}
}


public bool Drawdatagrid (Graphics g)
{

Try
{
Drawheader (g);
BOOL bcontinue = Drawrows (g);
return bcontinue;
}
catch (Exception ex)
{
MessageBox.Show (ex. Message.tostring ());
return false;
}

}

}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.