打印 C# 字符串数组时添加新行

本文关键字:添加 新行 数组 字符串 打印 | 更新日期: 2023-09-27 18:36:09

我有一个 c# winforms 应用程序,允许用户从DataGridView中选择/打印行。我无法弄清楚如何为每行创建一个新行。我已经为类似的问题找到了一些答案,但没有一个对我有用。

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);
    }
}

我尝试的所有内容都连接到我的字符串上

打印 C# 字符串数组时添加新行

向字符串添加新行非常简单

myString += Environment.NewLine;

但是,您需要确保用于显示字符串的内容正确呈现新行。