Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (cn.State == ConnectionState.Open)
            {
                cn.Close();
            }
            cn.Open();
            listView1.Clear();
            ImageList images = new ImageList();
            images.ColorDepth = ColorDepth.Depth32Bit;
            listView1.LargeImageList = images;
            listView1.LargeImageList.ImageSize = new System.Drawing.Size(255, 255);
            cmd.Connection = cn;
            cmd.CommandText = "select * from table6";
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            da.SelectCommand = cmd;
            dt.Clear();
            da.Fill(dt);
            foreach (DataRow row in dt.Rows)
            { 
                var image_buffer = (byte[])(row["picture"]);
                 MemoryStream image_stream = new MemoryStream(image_buffer);
                 image_stream.Write(image_buffer, 0, image_buffer.Length);
                images.Images.Add(row["picture"].ToString(), new Bitmap(image_stream));
           image_stream.Close();
                ListViewItem listItem = new ListViewItem();
                 listItem.Text = row["id"].ToString();            //t
                listItem.ImageKey = row["picture"].ToString();
                listView1.Items.Add(listItem);
      
            }


What I have tried:

this code display just first image in database i want display all
Posted
Updated 29-Mar-18 22:05pm
Comments
#realJSOP 24-Mar-18 18:10pm    
Maybe table6 only has one row of data.
kh medo 25-Mar-18 3:13am    
no has ten rows

Your biggest problem is the image key that you have chosen. Try adding this line, in the debugger, and examining the value of key:

string key = row["picture"].ToString()

I suspect, you'll find that it is always "System.Byte[]" or something similar. My guess is that you keep over-writing (and displaying) the one and only image, because you keep using the same image key.

You need to make sure you're using a unique key for each image. I'm not sure of the data type of row["id"]. It might suit your needs...try examining its ToString value. If its not good, you can always just add a row counter and use its value.

That said, I worry about some other problems. I'm hoping that you're simply cut-pasting some quick test code. Otherwise, you are missing a whole bunch of using statements (or Dispose calls).

Hope this helps. Good luck.
-Eric.
 
Share this answer
 
Comments
Christiaan van Bergen 30-Mar-18 16:21pm    
I think @Eric is right. The line :

images.Images.Add(row["picture"].ToString(), new Bitmap(image_stream));

will add the images in the collection with the same key, therefor only one ListItem will match with an image and display it.

It's quite unfortunate that the ImageList.ImageCollection.Add method does not throw a DuplicateKeyException, otherwise you would have found this one sooner.

Use a ListView with a UniformGrid in the ItemsPanelTemplate. I used the following to display 52 playing cards. You need, of course, to fill the collection first. This is for a WPF application


C#
<Grid HorizontalAlignment="Center"  >

           <ListView   ItemsSource="{Binding CardCollection }" >
               <ListView.ItemTemplate>
                   <DataTemplate >
                       <Grid HorizontalAlignment="Center" >
                           <Image   Source="{Binding CardGraphic}" />
                       </Grid>
                   </DataTemplate>
               </ListView.ItemTemplate>
               <ListView.ItemsPanel >
                   <ItemsPanelTemplate>
                       <UniformGrid Columns="13" HorizontalAlignment="Center"  />
                   </ItemsPanelTemplate>
               </ListView.ItemsPanel>
           </ListView>

       </Grid>
 
Share this answer
 
v2
Comments
Wendelius 30-Mar-18 4:19am    
Based on the properties, my guess is that the question is about Windows.Forms
George Swan 30-Mar-18 4:24am    
I've not used Windows.Forms in years. I'll leave it up in case someone finds it useful. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900