Show image in Listview when populating with an array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elmbrook
    New Member
    • Feb 2010
    • 15

    Show image in Listview when populating with an array.

    Hi

    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?

    Code:
    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("a aa", 0);
    ListViewItem item2 = new ListViewItem("b bb", 1);
    ListViewItem item3 = new ListViewItem("c cc", 0);

    //Add the items to the ListView.
    this.lstUser.It ems.AddRange(ne w ListViewItem[] { item1, item2, item3 });

    Any help is appreciated
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    First of all DON'T use Bitmap.FromFile
    It keeps a live link open with the file on HDD for as long as you are using it in your application.
    You are better off loading it as byte array into a completely memory resident Image object. That way you can delete or rename or do whatever you need to with the source file on the HDD.


    Second... Stop dealing with Array and ArrayList.
    Just use a List<ListViewIt em> as it is more coder-friendly to work with.

    Third... Did you notice that your working manual way adds to the ListView.Items collection, while your non-working code does not? Might be a clue there.

    Comment

    Working...