【发布时间】:2015-11-13 06:35:27
【问题描述】:
我正在尝试使用 For-Loop 将一些数据放入我的 Datagridview
但问题是,如果我的计数器无缘无故地为 2,我的循环就会跳出,我不知道为什么
这是我的代码:
var row = new DataGridViewRow();
//image directory
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Daniel\Pictures");
//getting all files from the Directory
foreach(FileInfo file in dir.GetFiles())
{
try
{
this.img16x16.Images.Add(Image.FromFile(file.FullName));
}catch
{
Console.WriteLine("This is not an image file");
}
}
for (int j = 0; j <= this.img16x16.Images.Count; j++)
{
dataGridView1.Rows[j].Cells[0].Value = img16x16.Images[j].ToString();
img16x16.ImageSize = new Size(16, 16);
dataGridView1.Rows[j].Cells[1].Value = img16x16.Images[j];
dataGridView1.Rows.Add(row);
感谢您的帮助
编辑:我找到了解决方案,我只需要输入这些 2:
var row = new DataGridViewRow();
dataGridView1.Rows.Add(row);
在for循环内我的代码前面
【问题讨论】:
-
一方面,您的循环最后会出现异常。
-
哦,是的,谢谢我没注意到
标签: c# for-loop datagridview