6

Background: I am working with winforms in c#. I do not want images to be shown in datagridview cells, i have stored only path in database and showing them in datagridview from database.

Problem: When user enters a cell a tooltip is poped-up. What I need is that when column index of current-cell is 2 then the tool tip should show an image from the path given in the current cell.

I have found This Article very good. But unable to get succeeded. I have following code

    void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
    {
        DataGridView parent = e.AssociatedControl as DataGridView;
        if (parent.CurrentCell != null)
        {
            if (parent.CurrentCell.ColumnIndex == 2)
            {
                Bitmap bmpIn = new Bitmap(parent.CurrentCell.Value + "");
                using (Graphics g = Graphics.FromImage(bmpIn))
                {
                    Rectangle mr = new Rectangle(5, 5, 50, 50);
                    mr.Location = new Point(5, 5);
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.DrawImage(bmpIn, mr);
                }
            }
        }
    }

I thought this code should draw image, but it is not drawing, and more than drawing I am uncertain about the location, even If I could draw, how to locate it in tootip. I have not been able to understand it from the article i mentioned. Below is the image of my datagridview.

enter image description here

Sami
  • 8,168
  • 9
  • 66
  • 99

3 Answers3

5

I did something similar for a project.

Instead, I just used a form which I set to open on CellMouseOver, close on CellMouseLeave

    frm_MouseOverPicture HoverZoom = new frm_MouseOverPicture();

    private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
       DataGridView dgv_sender = sender as DataGridView;
       DataGridViewCell dgv_MouseOverCell = dgv_sender.Rows[e.RowIndex].Cells[e.ColumnIndex];

       //Get FilePath from dgv_MouseOverCell content

       //Get x, y based on position relative to edge of screen
       //x, y = top left point of HoverZoom form

       HoverZoom.LoadPicture(FilePath);
       HoverZoom.Location = new System.Drawing.Point(x, y);
       HoverZoom.Show();

    }

    private void dgv_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
       HoverZoom.Hide();
       HoverZoom.ClearPicture();
    }

Hope that is close enough to what you are looking for. I just made the form with no border and put a picture box over the whole thing.

kschieck
  • 1,407
  • 2
  • 16
  • 29
  • Thanks... It was my last option as at least it works. I might have to go for it. – Sami Feb 13 '13 at 07:54
  • Accepted this answer as I am working with this solution, but it is not exactly what I asked. – Sami Mar 10 '13 at 16:10
2

Just add a picturebox to your form and set size mode to "StretchImage" and visible= false, then add the following events to your datagridview

       private void metroGrid1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv_sender = sender as DataGridView;
        DataGridViewCell dgv_MouseOverCell=null;
        if (e.RowIndex > 0 && e.ColumnIndex > 0 && e.RowIndex <dgv_sender.RowCount && e.ColumnIndex<dgv_sender.ColumnCount)
        {
          dgv_MouseOverCell = dgv_sender.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }
        if(dgv_MouseOverCell !=null)
        if (e.ColumnIndex == 4) {
            if (dgv_MouseOverCell.Value != null)
            {
                if (File.Exists(dgv_MouseOverCell.Value.ToString()))
                {
                    Image img = Image.FromFile(dgv_MouseOverCell.Value.ToString());
                    pictureBox1.ImageLocation = dgv_MouseOverCell.Value.ToString();
                    pictureBox1.Location = new System.Drawing.Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);
                    pictureBox1.Visible = true;
                }
            }
        }
    }

    private void metroGrid1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
        pictureBox1.Visible = false;
    }

Click here to view image

Ahmad Zahabi
  • 1,110
  • 12
  • 15
  • I'd advise that you add some context so that your answer is not just some code. Try to explain why it works, or works better, and what the OP has to watch out for. – Fabulous Jan 21 '18 at 13:48
1
    void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
    {
        DataGridView parent = e.AssociatedControl as DataGridView;
        if (parent.CurrentCell != null)
        {
            if (parent.CurrentCell.ColumnIndex == 2)
            {
                string path = parent.CurrentCell.Value.ToString();
                using (System.Drawing.Imaging.Metafile emf = new System.Drawing.Imaging.Metafile(path))
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(emf.Width, emf.Height))
                {
                    bmp.SetResolution(emf.HorizontalResolution, emf.VerticalResolution);

                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
                    {
                        g.DrawImage(emf,
                            new Rectangle(0, 0, emf.Width, emf.Height),
                            new Rectangle(0, 0, emf.Width, emf.Height),
                            GraphicsUnit.Pixel
                        );

                        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    }
                }
            }
        }
    }

Source

To use the System.Windows.Interop

"You have to download the .NET 3.0 runtime or later and install it to get the assembly..."

"After .NET 3.0 is installed, it should appear in the Add References list with component name as "WindowsBase". If it doesn't you can always add it from the Browse tab in the Add References dialog. (C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 on my box)"

Source

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44
  • Thanks. It looks better but please I am unable to make it working, although I have tried to windowbase.dll for Interop but still errors and if I comment the last line then no image appears in tooltip – Sami Feb 13 '13 at 08:01
  • I am using visual studio 2010. Dot Net Framework 4.0. Is your given direction right for that? If yes then please update the link as this link says `We are sorry, the page you requested cannot be found.` – Sami Feb 14 '13 at 17:25
  • @Sami, you have to [download](http://www.microsoft.com/en-us/download/details.aspx?id=25150) .net 3.5 SP1 and look for the dll. [here](http://msdn.microsoft.com/en-us/library/cc836417.aspx) the list of namespaces of .net 3.5 sp1 – spajce Feb 14 '13 at 22:09