我的代码打印数据网格中的问题 [英] problem in my code print datagrid
本文介绍了我的代码打印数据网格中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int i = 0;
string[] names=new string[] {"Amey","vijay","Ranjit","Bharat","saba"};
string[] tel = new string[] { "3232","32111","43434","3423","09221"};
public Form1()
{
InitializeComponent();
FillDataGridView();
}
private void FillDataGridView()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("tel");
for (int i = 0; i < 100 ; i++)
{
dr = dt.NewRow();
dr["Id"] = i + 1;
dr["Name"] = names[i % 5];
dr["tel"] = tel[i % 5];
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int width = 0;
int height = 0;
int x = 0;
int y = 0;
int rowheight = 0;
int columnwidth = 0;
StringFormat str=new StringFormat();
str.Alignment=StringAlignment.Near;
str.LineAlignment=StringAlignment.Center;
str.Trimming=StringTrimming.EllipsisCharacter;
Pen p=new Pen(Color.Black,2.5f);
//e.Graphics.DrawRectangle(p,dataGridView1.Bounds);
//printPreviewDialog1.Document=printDocument1;
//e.Graphics.DrawString("Hi this a test print", new Font(Font.SystemFontName,10.5f), Brushes.Black, new PointF(250.0f, 250.0f));
//e.Graphics.DrawImage((Image)bm,new Point(10,10));
#region Draw Column 1
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
e.Graphics.DrawString(dataGridView1.Columns[0].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);
#endregion
#region Draw column 2
e.Graphics.FillRectangle(Brushes.LightGray, new Rectangle(100 + dataGridView1.Columns[0].Width, 100 , dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height));
e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
e.Graphics.DrawString(dataGridView1.Columns[1].HeaderText, dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, 100, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);
width = 100 + dataGridView1.Columns[0].Width;
height = 100;
//variable i is declared at class level to preserve the value of i if e.hasmorepages is true
while (i < dataGridView1.Rows.Count)
{
if (height > e.MarginBounds.Height)
{
height = 100;
width = 100;
e.HasMorePages = true;
return;
}
height += dataGridView1.Rows[i].Height;
e.Graphics.DrawRectangle(Pens.Black, 100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[0].FormattedValue.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);
e.Graphics.DrawRectangle(Pens.Black, 100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Font, Brushes.Black, new RectangleF(100 + dataGridView1.Columns[0].Width, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height), str);
width += dataGridView1.Columns[0].Width;
i++;
}
#endregion
}
I can not draw column 3
please help me?!
推荐答案
您似乎也没有画第2列:在整个代码中,您引用的是dataGridView1.Columns[0]
始终是第一列.修复该代码,使其首先起作用,然后开始查看您的其他问题是什么!
顺便说一句:
我无法绘制第3列"没有帮助-尝试告诉我们什么是阻止您执行此操作的方法.
BTW2:
请不要在方法中的代码块中使用区域-使用注释块,或者如果代码太大,则将其移动到单独的方法中.区域对代码模块化无济于事,而对可读性却有帮助-仅当用于将相关模块分组在一起时.
You don''t appear to be drawing column 2, either: Throughout that code you refer todataGridView1.Columns[0]
which is always the first column. Fix that code so it works first, then start looking at what your other problems are!
BTW:
"I can not draw column 3" is not helpful - try telling us what isa stopping you doing it.
BTW2:
Please don''t use regions for blocks of code within a method - use comments blocks, or move the code into separate methods if it is getting too large. Regions do not help with code modularity, they help with readability - but only when used to group related modules together.
这篇关于我的代码打印数据网格中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文