怎样打印datagrid表

521kevin 2004-04-25 08:22:03
怎样打印datagrid表
在printdocument_printpage中怎么写?最好写出代码
...全文
76 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shdragon7 2004-04-27
  • 打赏
  • 举报
回复
xdwlb(王立宾) 你好:
你说
建议用c1turedbgrid或c1flexgrid,操作非常简单,打印只需要一条命令。
能不能给出一个例子,我在msdn里找不到你所说的,本人愚笨
cg1120II 2004-04-25
  • 打赏
  • 举报
回复
if (endColumn <= startColumn)
endColumn = tableStyle.GridColumnStyles.Count;

if (endRow <= startRow)
endRow = rowCount;

// 开始绘制内容。
bounds.Y += fontHeight * 3 / 2;
font = dataGrid.Font;
fontHeight = font.Height;

bounds.Height = fontHeight * 3 / 2;
for(int row=startRow; row<rowCount; row++)
{
// 定位到第一列。
bounds.X = e.MarginBounds.Left;
for(int column=startColumn; column<endColumn; column++)
{
DataGridColumnStyle columnStyle = tableStyle.GridColumnStyles[column];

// 设置列宽度。
bounds.Width =(int)(columnStyle.Width*btWidth);

// 绘制单元格。
Pen pen = column == startColumn ? outerPen : innerPen;
g.DrawLine(pen, bounds.Left, bounds.Bottom, bounds.Left, bounds.Top);
g.DrawLine(innerPen, bounds.Left, bounds.Top, bounds.Right, bounds.Top);
Paint(g, bounds, manager, row, columnStyle, SystemBrushes.Window, brush);

// 移到下一列。
bounds.X += bounds.Width;
}

// 绘制右边界。
g.DrawLine(outerPen, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom);

// 移动到下一行。
bounds.Y += bounds.Height;

// 超出了下边距。
if (bounds.Bottom > e.MarginBounds.Bottom-4*fontHeight)
{
endRow = row;
break;
}
}

// 绘制下边界。
g.DrawLine(outerPen, e.MarginBounds.Left, bounds.Top, bounds.Left, bounds.Top);

Font bottomFont = new Font("宋体",10,FontStyle.Regular);
g.DrawLine(new Pen(brush,1),new Point(e.MarginBounds.Left,e.MarginBounds.Bottom-fontHeight*2),new Point(e.MarginBounds.Right,e.MarginBounds.Bottom-fontHeight*2));
currentPage++;
// format.Alignment = StringAlignment.Center;
// g.DrawString(String.Format("第 "+currentPage.ToString()+" 页"),bottomFont,brush,new Point((e.MarginBounds.Right-(int)Math.Ceiling(g.MeasureString(String.Format("第 "+currentPage.ToString()+" 页"),bottomFont).Width))/2,e.MarginBounds.Bottom-fontHeight),format);
format.Alignment = StringAlignment.Far;
g.DrawString("C#打印DataGrid报表",bottomFont,brush,new Point(e.MarginBounds.Right,e.MarginBounds.Bottom-fontHeight),format);

// 有后续页。
if (endColumn < tableStyle.GridColumnStyles.Count || endRow < rowCount-1)
{
e.HasMorePages = true;

// 先列分页,后行分页。
if (endColumn < tableStyle.GridColumnStyles.Count)
{
startColumn = endColumn;
}
else
{
startColumn = 0;
startRow = endRow+1;
}
}
}
}
format.Dispose();
brush.Dispose();
base.OnPrintPage(e);
}

// row: -1 表示绘制 HeaderText。
private void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, DataGridColumnStyle columnStyle, Brush backBrush, Brush foreBrush)
{
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
format.LineAlignment = StringAlignment.Center;
string text;
int width;
Font font;

if (rowNum == -1)
{
font = dataGrid.HeaderFont;
text = columnStyle.HeaderText;
width= (int)Math.Ceiling(g.MeasureString(text,font,(int)(columnStyle.Width*btWidth)).Width);
bounds.X += (bounds.Width-width)/2;
}
else
{
font = dataGrid.Font;

try
{
DataRowView row = source.List[rowNum] as DataRowView;
object value = row[columnStyle.MappingName];
TypeConverter converter = columnStyle.PropertyDescriptor.Converter;
text = converter.ConvertToString(columnStyle as ITypeDescriptorContext, System.Globalization.CultureInfo.CurrentCulture, value);
if (text == null) text = columnStyle.NullText;
}
catch
{
text = String.Empty;
}
width= (int)Math.Ceiling(g.MeasureString(text,font,(int)(columnStyle.Width*btWidth)).Width);
bounds.X += (bounds.Width-width)/2;
}
// 截断换行后面的部分
if(text.IndexOf('\n') >-1)
text = text.Substring(0,text.IndexOf('\n'));
g.DrawString(text, font, foreBrush, bounds, format);
}
}
}
cg1120II 2004-04-25
  • 打赏
  • 举报
回复
这是一个打印类,你可以参考一下:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;

namespace XM120.Forms
{
/// <summary>
/// DataGridDocument 的摘要说明。
/// </summary>
public class PrintDocument : System.Drawing.Printing.PrintDocument
{
private System.Windows.Forms.DataGrid dataGrid;
private int startRow, endRow;
private int startColumn, endColumn;
private float btWidth;// 列与打印页面的宽度比例
private int currentPage;

public PrintDocument()
{
}

/// <summary>
/// 获取或设置打印的数据网格。
/// </summary>
/// <remarks>数据网格的Tag属性携带DateTime[]。分别为StartTime和EndTime,作为时间段。</remarks>
public System.Windows.Forms.DataGrid DataGrid
{
get { return dataGrid; }
set
{
dataGrid = value;
if (dataGrid != null && dataGrid.CaptionText != String.Empty)
DocumentName = dataGrid.CaptionText;
}
}

protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint (e);

startColumn = endColumn = 0;
startRow = endRow = currentPage = 0;
}


protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
{
e.PageSettings.Margins = DefaultPageSettings.Margins;
}

protected override void OnPrintPage(PrintPageEventArgs e)
{
Graphics g = e.Graphics;

// 创建黑色笔刷。
Brush brush = new SolidBrush(SystemColors.WindowText);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
format.Alignment = StringAlignment.Center;
if (dataGrid == null)
{
using(Font font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))))
g.DrawString("未绑定数据网格。", font, brush, e.MarginBounds, format);
}
else
{

Rectangle bounds = e.MarginBounds;

// 绘制文档标题。
Font font = dataGrid.CaptionFont;
int fontHeight = font.Height;
bounds.Height = fontHeight;
g.DrawString(DocumentName, font, brush, bounds, format);

// 绘制时间段和制表日期。
bounds.Y += fontHeight * 2;
font = dataGrid.Font;
fontHeight = font.Height;
bounds.Height = fontHeight * 3 / 2;
DateTime[] array = dataGrid.Tag as DateTime[];
format.LineAlignment = StringAlignment.Center;
if (array != null && array.Length >= 2)
{
format.Alignment = StringAlignment.Near;
g.DrawString(String.Format("时间段:{0:yyyy年M月d日}-{1:yyyy年M月d日}", array[0], array[1]), font, brush, bounds, format);
g.DrawLine(new Pen(brush,1),new Point(bounds.X+(int)Math.Ceiling(g.MeasureString("时间段8",font).Width),bounds.Y+fontHeight+1),new Point(bounds.X+(int)Math.Ceiling(g.MeasureString(String.Format("时间段:{0:yyyy年M月d日}-{1:yyyy年M月d日}", array[0], array[1]),font).Width),bounds.Y+fontHeight+1));
}
format.Alignment = StringAlignment.Far;
g.DrawString(String.Format("制表日期: {0:yyyy年M月d日}", DateTime.Now), font, brush, bounds, format);
g.DrawLine(new Pen(brush,1),new Point(e.MarginBounds.Right-(int)Math.Ceiling(g.MeasureString(String.Format("{0:yyyy年M月d日}", DateTime.Now),font).Width),bounds.Y+fontHeight+1), new Point(e.MarginBounds.Right,bounds.Y+fontHeight+1));

DataGridTableStyle tableStyle = null;
int rowCount = 0;
CurrencyManager manager = (CurrencyManager)dataGrid.BindingContext[dataGrid.DataSource, dataGrid.DataMember];
if (manager != null && manager.List is ITypedList)
{
string mappingName = (manager.List as ITypedList).GetListName(null);
if (dataGrid.TableStyles.Contains(mappingName))
tableStyle = dataGrid.TableStyles[mappingName];

// 获取行数。
rowCount = manager.Count;
}

if (tableStyle != null)
{
// 移动到下一行,1。5倍行距。
bounds.Y += fontHeight * 3 / 2;
font = dataGrid.HeaderFont;
fontHeight = font.Height;
bounds.Height = fontHeight * 3 / 2;

// 定位到起始列。
bounds.X = e.MarginBounds.Left;

Pen outerPen = new Pen(brush, 2);
Pen innerPen = new Pen(brush, 1);


btWidth =0;
int i;
for(i = startColumn; i < tableStyle.GridColumnStyles.Count;i++)
{
btWidth += tableStyle.GridColumnStyles[i].Width;
if(btWidth >= e.MarginBounds.Width)
{
btWidth -= tableStyle.GridColumnStyles[i].Width;
break;
}
}

endColumn = i;

if(btWidth != 0 && btWidth <= e.MarginBounds.Width)
if(btWidth > e.MarginBounds.Width/2)
btWidth = (float)e.MarginBounds.Width/btWidth;
else
btWidth = (float)e.MarginBounds.Width/(2*btWidth);
else
btWidth = 1;

// 绘制列标题。
for(int column=startColumn; column<endColumn; column++)
{
DataGridColumnStyle columnStyle = tableStyle.GridColumnStyles[column];

// format.Alignment = c
// 设置列的宽度。
bounds.Width = (int)(columnStyle.Width*btWidth);

// 列的范围超出了右边距。
// if (bounds.Right > e.MarginBounds.Right)
// {
// endColumn = column;
// break;
// }

// 绘制单元格。
Pen pen = column == startColumn ? outerPen : innerPen;
g.DrawLine(pen, bounds.Left, bounds.Bottom, bounds.Left, bounds.Top);
g.DrawLine(outerPen, bounds.Left, bounds.Top, bounds.Right, bounds.Top);
Paint(g, bounds, manager, -1, columnStyle, SystemBrushes.Window, brush);

// 移动到下一列。
bounds.X += bounds.Width;
}

// 绘制右边界。
g.DrawLine(outerPen, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom);

cg1120II 2004-04-25
  • 打赏
  • 举报
回复
用水晶报表不可以吗?把水晶报表的数据源联接你的DATAGRID相同的数据源就行了
xdwlb 2004-04-25
  • 打赏
  • 举报
回复
建议用c1turedbgrid或c1flexgrid,操作非常简单,打印只需要一条命令。
wangjian 2004-04-25
  • 打赏
  • 举报
回复
我论坛有代码,去看看:)WWW.SHSOFTS.COM

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧