2

I am using a listview control where I am adding Images through ImageList

if (dtPhoto.Rows.Count > 0)
{
foreach (DataRow dr in dtPhoto.Rows)
            {
                if (dr["Photo"].ToString() != "")
                {
                    byte[] barrImg = (byte[])dr["Photo"];
                    MemoryStream mStream = new MemoryStream(barrImg);
                    imageList.Images.Add(Image.FromStream(mStream));
                }
            }
        }

        imageList.ImageSize = new Size(75, 75);
        lvItem.LargeImageList = imageList;

        for (int i = 0; i < imageList.Images.Count; i++)
        {
            lvItem.Items.Add(dtPhoto.Rows[i]["Name"].ToString() + "\n" + "(" + dtPhoto.Rows[i]["Type"].ToString() + ")", i);
        }

I want to add border to each item in the Listview Is it possible? Please Help. Thanks in advance.

Adarsh Ravi
  • 893
  • 1
  • 16
  • 39

1 Answers1

3

You can set OwnerDraw property of ListView to true, then handle DrawItem event and draw the border, for example:

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    e.DrawDefault = true;
    e.Graphics.DrawRectangle(Pens.Red, e.Bounds);
}

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks It worked. But it gives a fixed size to all the images is there a way around for it? – Adarsh Ravi Sep 21 '15 at 12:32
  • @AdarshRavi You are welcome, the main way to show images in `ListView` is using `ImageList` that is has all images in same size, you can use `DrawItem` event to completely draw items yourself or draw your images of different size on center of a bitmap of fixed size (Same size as ImageList) and then put them in ImageList. An example of what I said is showing thumbnails of images of different size while all thumbnails are showing in 128x128 size. – Reza Aghaei Sep 21 '15 at 14:55
  • 2
    @AdarshRavi as a better workaround if you can use a different control than ListView, Create a `UserControl` that hosts a `PictureBox` and a `Label` (like an item of a `ListView`) then layout as many as your custom created control using a `FlowLayoutPanel`. – Reza Aghaei Sep 21 '15 at 14:58