2016-04-01 57 views
1

我在项目上遇到了一些问题。C#WinForms中列表视图的第一列中的图像

正如您在图片中看到的,我有一个ListView,其中一些数据从类人员的对象列表中拉出。这个类还有一个属性,用于保存用户选择的图像的路径,该图像应该显示在ListView的第一列,它表示teste。

enter image description here

我试过下面的代码:

ImageList imgs = new ImageList(); 
imgs.ImageSize = new Size(40, 40); 
foreach (user list in Global.userslist) 
{ 
    imgs.Images.Add(Image.FromFile(list.Photo)); 
} 
listview1.SmallImageList = imgs; 
foreach (user list in Global.userslist) 
{ 
    imageList1.Images.Add(Image.FromFile(list.Photo)); 
    listview1.Items.Add(new ListViewItem(new string[] { "teste", Convert.ToString(list.Id), lista.Name, list.Birthday.ToString("dd/MM/yyyy") })); 
} 
+0

为什么你不用'DataGridView'来代替? –

+0

我想过这个,但是这个ListView已经做了很多工作,我似乎无法找到如何使用DataGridView进行累积搜索。 –

+1

你在上面的代码中的问题是你忘了设置你的项目的'ImageIndex'。 –

回答

1

您应该设置ImageIndexImageKey属性来显示图像的项目。

您可以通过ListViewItemconstructor的索引或密钥。您也可以使用ImageIndexImageKey属性分配值。

在你的代码,你可以将它们添加到ImageList当图像键:创建项目时

imgs.Images.Add(list.Id.ToString(), Image.FromFile(list.Photo)); 

然后,使用ImageKey

listview1.Items.Add(new ListViewItem(new string[] { ... ... }, list.Id.ToString())); 
+0

非常感谢。问题解决了。 –

+0

不客气。 –