简体   繁体   English

如何使用 C# 中的绘制字符串将所有行值打印到文档

[英]How can i print all row value of to a Document using Draw String in C#

I have tried this code, but it showing outofrange exception, how can I resolve it, pls help我试过这段代码,但它显示超出范围的异常,我该如何解决它,请帮助

try
{
    for (int i = 0; i < Convert.ToInt32(OrderGV.Rows.Count)-1; i++)
    {
       graphics.DrawString(OrderGV.SelectedRows[i].Cells[1].Value.ToString() + "\t" + OrderGV.SelectedRows[i].Cells[2].Value.ToString(), new Font("Century", 14, FontStyle.Bold), Brushes.Black, new Point(startx, starty+200));
    }
}
catch (Exception)
{
   throw;
}

***Here OrderGV is a Datagridview. ***这里的 OrderGV 是一个 Datagridview。

You are looping over all data rows, but your drawing function only uses selected rows which causes an error if not all rows are selected.您正在遍历所有数据行,但您的绘图 function 仅使用选定的行,如果未选择所有行,则会导致错误。 So the problem is, the Rows array in your loop is bigger than the SelectedRows array.所以问题是,循环中的 Rows 数组比 SelectedRows 数组大。 I think the following should work.我认为以下应该有效。

try
{
    for (int i = 0; i < Convert.ToInt32(OrderGV.Rows.Count)-1; i++)
    {
       graphics.DrawString(OrderGV.Rows[i].Cells[1].Value.ToString() + "\t" + OrderGV.Rows[i].Cells[2].Value.ToString(), new Font("Century", 14, FontStyle.Bold), Brushes.Black, new Point(startx, starty+200));
    }
}
catch (Exception)
{
   throw;
}

Or the other way around if you only want the selected rows或者相反,如果您只想要选定的行

try
{
    for (int i = 0; i < Convert.ToInt32(OrderGV.SelectedRows.Count)-1; i++)
    {
       graphics.DrawString(OrderGV.SelectedRows[i].Cells[1].Value.ToString() + "\t" + OrderGV.SelectedRows[i].Cells[2].Value.ToString(), new Font("Century", 14, FontStyle.Bold), Brushes.Black, new Point(startx, starty+200));
    }
}
catch (Exception)
{
   throw;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM