DataGridViewRowCollection打印问题(新行没有出现)

时间:2016-04-08 09:41:52

标签: c# printing datagridview

所以我有这个代码块来打印DataGridview行的集合。我想在一条单独的行中打印每一行,但它们都打印在同一行中。任何的想法?

see the screenshot

foreach (DataGridViewRow row in rows)
{

DataRow myRow = (row.DataBoundItem as DataRowView).Row;

string myStr = string.Join("|", myRow.ItemArray.Select(p => p.ToString()).ToArray());

myStr += Environment.NewLine;

graphics.DrawString(myStr, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + Offset);

}

1 个答案:

答案 0 :(得分:1)

您没有递增startY坐标。

添加

startY = startY + 20;

graphics.DrawString(...)

如果您出于某种原因想要为每一行创建一个大字符串,请按以下方式进行:

string myStr = string.Empty;

foreach (DataGridViewRow row in rows)
{
    DataRow myRow = (row.DataBoundItem as DataRowView).Row;
    string myStr = string.Join("|", myRow.ItemArray.Select(p => p.ToString()).ToArray());
    myStr += "\r\n";
}

graphics.DrawString(myStr, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + Offset);