添加新行,当印刷c#字符串数组 - Add new line when printing C# string array

- 此内容更新于:2015-12-16
主题:

我有一个c#winforms应用程序,允许用户从选择/打印行。我不能解决如何为每一行创建一个新行。我发现一些类似问题的答案但没有为我工作。所有我尝试只是连接字符串

原文:

I have a c# winforms app that allows users to select / print rows from a DataGridView. I can't work out how to create a new line for each row. I have found a few answers for similar questions but none that work for me.

private void printDocument_PrintPage(object sender, PrintPageEventArgs ev)
{
    Graphics graphic = ev.Graphics;
    DataGridViewSelectedRowCollection rows = dataGridView1.SelectedRows;
    foreach (DataGridViewRow row in rows)
    {
        DataRow myRow = (row.DataBoundItem as DataRowView).Row;
        string myStr = string.Join( "|", myRow.ItemArray.Select( p => p.ToString( ) ).ToArray( ));
        //myStr += "/n/r"; 
        graphic.DrawString(myStr, new Font("Times New Roman", 10, FontStyle.Regular), Brushes.Black, 20, 225);
    }
}

Everything I try just concatenates on to my string

网友:另外每一行都有自己的声明,同时增加根据位置。

(原文:Alternatively each line has its own DrawString() statement while increasing the y location according to graphic.MeasureString(...).Height.)

网友:或画出文本使用矩形布局,让系统创建一个段落的外观包装线。这一切都取决于想要的结果。

(原文:Or draw the text using a layout rectangle and let the system wrap lines to create a paragraph look. It all depends on want the result to be.)

网友:@ja72非常感谢你的帮助。

(原文:@ja72 Thanks very much for your help.)

解决方案:
添加一个新的行字符串非常简单,然而,你需要确保你使用什么来显示字符串正确地呈现新行。
原文:

Adding a new line to a string is as simple as

myString += Environment.NewLine;

However, you will need to make sure that what you're using to display the string correctly renders new lines.