473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. this.lstUser.View = View.Details;
  3.             this.lstUser.GridLines = true;
  4.             lstUser.HeaderStyle = ColumnHeaderStyle.None;
  5.             lstUser.Columns[0].Width = lstUser.Width - 4;
  6.  
  7.             mydb thedb = new mydb(); //Executes the SQL statement
  8.             SqlDataReader thereader = null;
  9.  
  10.             string _user = "";
  11.             ArrayList myarray = new ArrayList();
  12.             string _sql = "select * from sysUser order by LogName";
  13.  
  14.             thereader = thedb.SQLExecute_Reader(_sql);
  15.  
  16.             if ((thereader != null) && (thereader.HasRows == true))
  17.             {
  18.                 while (thereader.Read())
  19.                 {
  20.  
  21.                     _user = thereader["logname"].ToString();
  22.                     myarray.Add(new ListViewItem(new string[] { _user }));
  23.                 }
  24.             }
  25.             if (thereader != null)
  26.             { thereader.Close(); }
  27.  
  28.             this.lstUser.Items.AddRange((ListViewItem[])myarray.ToArray(typeof(ListViewItem)));
  29.  
  30.             // Create two ImageList objects.
  31.             ImageList imageListSmall = new ImageList();
  32.             ImageList imageListLarge = new ImageList();
  33.  
  34.             // Initialize the ImageList objects with bitmaps.
  35.             imageListSmall.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
  36.             imageListSmall.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
  37.             imageListLarge.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
  38.             imageListLarge.Images.Add(Bitmap.FromFile(myglobal_properties.FP1 + @"media\\image\\user.bmp"));
  39.  
  40.             //Assign the ImageList objects to the ListView.
  41.             this.lstUser.LargeImageList = imageListLarge;
  42.             this.lstUser.SmallImageList = imageListSmall;  
  43.  

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
Mar 15 '10 #1
1 4563
tlhintoq
3,525 Expert 2GB
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<ListViewItem> 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.
Mar 16 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Juan | last post by:
When populating a dataset I get an error if the data being retrieved includes a string field with a Null value: "Not valid store type: DBNull". What kinf of data type can i define in the dataset to...
6
by: Trint Smith | last post by:
How can I show image thumbnail?? thanks, Trint ..Net programmer trintsmith@hotmail.com *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get...
1
by: active | last post by:
I've been working on a problem for a few days now. I do not get a Double-Click event fired for a ListView when I double click. I now find that if I double click with the right button it works OK....
2
by: Don | last post by:
Is there any way to convert an Image into a Byte array that does not require saving the image to disk? - Don
1
by: basavaraj koti | last post by:
I need to show image using xslt Below provided in my xml and xslt. <?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl" href="../xyz.xsl"?> <Grade class="03"...
8
by: anandmms | last post by:
Hello friend, my problem is to save more than one fingerprint images in one byte array and then store in database(oracle). i dont know how to solve it. But previously i had did image saving...
4
by: 200dogz | last post by:
Hi guys, Just a quick and simple question: i'm trying to have a loading image displayed when a submit button is pressed. Basically what I have at this stage is an asp:image with its...
4
by: waqasahmed996 | last post by:
hi to all i want to show image on page in fix size of 300*300. images which have size more than that should also show in that size with same picture quality. is it possible? if yes how can i do...
2
by: slao | last post by:
I am unable to get the updated value of textbox within a listview when "Update" link button is clicked which calls a function. <asp:ListView ID="_lvCartItems"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.