I am trying to add an image to a listview box. I have the below code and according to me this should work. However, the image does not show at all. I can add an image when I manually add each item but not when I add the items as an array.
Can someone please tell me what is going wrong?
Expand|Select|Wrap|Line Numbers
- this.lstUser.View = View.Details;
- this.lstUser.GridLines = true;
- lstUser.HeaderStyle = ColumnHeaderStyle.None;
- lstUser.Columns[0].Width = lstUser.Width - 4;
- mydb thedb = new mydb(); //Executes the SQL statement
- SqlDataReader thereader = null;
- string _user = "";
- ArrayList myarray = new ArrayList();
- string _sql = "select * from sysUser order by LogName";
- thereader = thedb.SQLExecute_Reader(_sql);
- if ((thereader != null) && (thereader.HasRows == true))
- {
- while (thereader.Read())
- {
- _user = thereader["logname"].ToString();
- myarray.Add(new ListViewItem(new string[] { _user }));
- }
- }
- if (thereader != null)
- { thereader.Close(); }
- this.lstUser.Items.AddRange((ListViewItem[])myarray.ToArray(typeof(ListViewItem)));
- // Create two ImageList objects.
- ImageList imageListSmall = new ImageList();
- ImageList imageListLarge = new ImageList();
- // Initialize the ImageList objects with bitmaps.
- imageListSmall.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
- imageListSmall.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
- imageListLarge.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
- imageListLarge.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
- //Assign the ImageList objects to the ListView.
- this.lstUser.LargeImageList = imageListLarge;
- this.lstUser.SmallImageList = imageListSmall;
When I add the items manually the image works.
ListViewItem item1 = new ListViewItem("aaa", 0);
ListViewItem item2 = new ListViewItem("bbb", 1);
ListViewItem item3 = new ListViewItem("ccc", 0);
//Add the items to the ListView.
this.lstUser.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
Any help is appreciated