2014-03-30 42 views
0

编译我的网格视图以显示一组订单时,我得到索引超出范围异常。c#>索引超出范围。必须是非负数并且小于集合的大小

它不返回所有行。它只返回一行

任何帮助,非常感谢。

try 
       { 
        oClient.Connect(oServer); 
        MailInfo[] infos = oClient.GetMailInfos(); 
        Console.WriteLine(infos.Length); 
        for (int i = 0; i < infos.Length; i++) 
        { 
         MailInfo info = infos[i]; 
         Mail oMail = oClient.GetMail(info); 

         dgView_Inbox.Rows[i].Cells[0].Value = oMail.From.ToString(); 
         dgView_Inbox.Rows[i].Cells[1].Value = oMail.Subject.ToString(); 

        } 
        oClient.Quit(); 


       } 
       catch (Exception ep) 
       { 
        Console.WriteLine(ep.Message); 
       } 
+2

在你得到什么线异常? –

+0

你的datagridview中有没有行? – Steve

+0

你有多少个预定义的行,你有邮件吗? – Dunken

回答

0

在循环插入一个Add命令,并使用其索引:

int newrow = dgView.Rows.Add(); 
dgView_Inbox.Rows[newrow ].Cells[0].Value = oMail.From.ToString(); 
dgView_Inbox.Rows[newrow ].Cells[1].Value = oMail.Subject.ToString(); 
相关问题