views:

24

answers:

0

I have a potential of up to 4 images per database row/entry and I am querying on Gridview_currentRowChanged. The issue was how to clear the picturebox(s) before loading the next row.

   private void radGridView1_CurrentRowChanged(object sender, Telerik.WinControls.UI.CurrentRowChangedEventArgs e)
        {
            panelImages.Controls.Clear();
            if (radGridView1.MasterGridViewInfo.CurrentRow != null)
            {
                // List the Images we require.
                List<string> ImageList = new List<string>();
                int i = 1;
                while (i <= 4)
                {
                    string image = Convert.ToString(radGridView1.CurrentRow.Cells["image" + i].Value);
                    if (string.IsNullOrEmpty(image) == false)
                    {
                        ImageList.Add(image);
                        i++;
                    }
                    else
                    {
                        i = 5;
                    }
                }
                // Now download the required images
                int k = 0;
                foreach (string ListedImage in ImageList)
                {
                    System.Net.WebClient Client = new WebClient();
                    byte[] data = Client.DownloadData("http://autosparesuk.net/images/Items/" + ListedImage + "");
                    string img_thu = "http://example.net/images/Items/" + ListedImage + "";
                    MemoryStream ms = new MemoryStream(data);
                    Bitmap objBitMap = new Bitmap(ms);
                    PictureBox pb = new PictureBox();
                    pb.Size = new Size(65, 65);
                    pb.Location = new Point((k * 72), 5 + 2);
                    pb.BorderStyle = BorderStyle.Fixed3D;
                    pb.ImageLocation = img_thu;
                    pb.Cursor = System.Windows.Forms.Cursors.Hand;
                    pb.SizeMode = PictureBoxSizeMode.StretchImage;
                    panelImages.Controls.Add(pb);
                    pb.BringToFront();
                    pb.Image = objBitMap;
                    k++;
                }
            }

        }

Still not convinced this is the cleanest way to accoplish this, but it works.