我做的打印窗体控件和DataGrid控件的,要不要?
解决方案 »
- C#、数据库初学者学习方法
- form里有个panel,panle里有个button,请问怎么得到这个button在form里的坐标。
- C#登陸方案
- TreeView的小问题,你知道的帮帮我啊
- 关于数据分块的实现原理和方法
- 在线等待,高分求解,如何修改编译好的Winform程序?
- C#中怎么给一个控件写自定义事件,比如当TextBox的Tag属性更改时触发。除了用Timer控件
- C#学习书籍推荐
- 求教:谢谢:我现在用的定时器的问题。 System.Timers.Timer。
- 大神都来说说 “接口” 吧
- 代码重用问题!!!
- 水晶报表,两个字段可能其中有一个或全为NULL(SQL--NULL),如何相加
http://www.codeproject.com/useritems/datagridex.asp?target=datagrid%7Cprint
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Collections;
using System.IO;namespace PrintDataGridApp
{ /// ************************ DataGridPrinter ********************
/// By Michael Gold
/// Copyright 2002. All Rights Reserved
/// *********************************************************
/// <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;
public System.Collections.ArrayList Lines; int PageWidth;
int PageHeight;
int TopMargin;
int BottomMargin; public DataGridPrinter(DataGrid aGrid, PrintDocument aPrintDocument, DataTable aTable)
{
//
// TODO: Add constructor logic here
//
TheDataGrid = aGrid;
ThePrintDocument = aPrintDocument;
TheTable = aTable; PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom; } 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; int columnwidth = PageWidth/TheTable.Columns.Count; 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 + kVerticalCellLeeway);
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();
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; int initialRowCount = RowCount; RectangleF RowBounds = new RectangleF(0, 0, 0, 0); // draw vertical lines
// draw the rows of 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 + kVerticalCellLeeway);
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 + kVerticalCellLeeway),
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+TopMargin))
{
//DrawHorizontalLines(g,
DrawHorizontalLines(g, Lines);
DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
return true;
}
} DrawHorizontalLines(g, Lines);
DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
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;
} } }}
标题自己画