okay I have been looking all over the web trying certain pieces of code and I have had no luck. I am tired and I really am at a lost right now. I am trying to make it so that when the user mouses over a select set of cells (they are all in the same column, just each row would display a different tooltip outcome). Here is the code let me explain some more after i post this class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WorkSchedule_App_1v3
 {
//=======================================================================================
  public partial class employeeAlterForm4 : Form
   {
    static readInFileDel2 readInFileCall = new readInFileDel2
                                           (readInFileClass2.readInFile2);
    static Employee [] employeeTempArray;
//---------------------------------------------------------------------------------------
    public employeeAlterForm4()
     {
      InitializeComponent();

      readInFile();
      bool supHold = false;

      dataGridView1.ColumnCount = 4;
      dataGridView1.Columns [0].Name = "Employee Name";
      dataGridView1.Columns [1].Name = "Date of Birth - Age";
      dataGridView1.Columns [2].Name = "Position";
      dataGridView1.Columns [3].Name = "Shift Types";

      for (int i = 0; i < employeeTempArray.Length - 1; i++)
       {
        dataGridView1.Rows.Add();
        dataGridView1 [0, i].Value = employeeTempArray [i].isName();
        dataGridView1 [1, i].Value = employeeTempArray [i].isDOB();

        supHold = employeeTempArray [i].isSup();
        if (supHold == true)
         {
          dataGridView1 [2, i].Value = "Supervisor";
         }
        else 
         {
          dataGridView1 [2, i].Value = "Employee";
         }
       }
     }
//---------------------------------------------------------------------------------------
    public static void readInFile() //reads the employee File
     {
      employeeTempArray = readInFileCall("employeeSave.txt"); 
     }
//---------------------------------------------------------------------------------------
   }
//=======================================================================================
 }

Okay and now let me include a screen of what it looks like (what this code produces)

http://i109.photobucket.com/albums/n65/Vin_diesalxxx/dataGridViewtooltiphelp.jpg
(I erased out the last name of the first person that is not an error)

Now what I want it to do is that in this employeeTempArray you see is a method

employeeTempArray[index].isAge()

Which returns the age of the person who is in the system. Well what I want this code to do is that when you mouse over the persons Date Of Birth they get their age (so I would have to find a way to pull up the index of the array corresponding to the row and then display this stubborn method from a static class into a private class...stupid forms, as a tooltip). I would love it so that it would change automatically if I hover over a new date (the tooltip being displayed where the mouse is located). I know some C# programming (althought I don't consider myself an expert), but I hope you can help me? I have been search the web and am finally calling it quits, I am tired and need some sleep. Thanks in advance to all the help you can give.

Oh also if anyone can help me find a way that when I click the date of birth to sort, that it will sort by the age (cause that dob is a string)

Recommended Answers

All 12 Replies

To get a different tool tip for each cell, trap the CellMouseEnter event, use e.RowIndex and e.ColumnIndex to identify the cell and then use a ToolTip to display the tip text you want. However, on the ToolTip.Show method use the container of the DataGrid as the window; as using the DataGrid can sometimes hide the tip.

Regarding sorting the column. Have you tried converting the loaded DoB to a DateTime object before loading in the DataGrid.
It might just then do the sort properly when you click on the column header.

Yeah that tooptip show how exactly do I use that I am not familiar with the tooptip item (first time using it)

I mean I tried this code but it doesn't work (did work with MessageBox.Show). Oh also I assigned the event to the form from the designer side

private void dataGridView1_CellMouseEnter (object sender, DataGridViewCellEventArgs e)
     {
      if ((e.RowIndex != -1) && (e.ColumnIndex == 1))
      {
       //MessageBox.Show("TEST");
       ToolTip tooltip1 = new ToolTip();
       tooltip1.Show("hello", dataGridView1, Cursor.Position.X, Cursor.Position.Y);
      }
     }

I did say not to use the DataGridView as the window for the tooltip.
Not sure why, but using DataGridView blocks the tooltip from showing.
The easiest way to make sure your X and Y are the same is to full dock the DataGridView in a panel or other container and use the container as the window reference for the tooltip. (Also note the X and Y are relative to the window object you use)

Make sure your ToolTip object is not local to the method.
Make it form level scope, otherwise the tooltip object may be disposed before the tip is shown. (Tooltip.Show does not block like MessageBox.Show).

can I get an example code cause I am confused (no this is not for an assignment just personal use for fun). I am really confused on how to use the toopTip I must be using it completely wrong

plz help!

I have no clue how to use ToolTips I can't even get them to display (see above posts)

There are a couple of other ways to get tooltips on DataGridView.

Each cell has its own tooltip in the cells DataGridViewCell.ToolTipText property.
E.g. dataGridView1[2, 3].ToolTipText = "Cell Tip Text"; This is fine if the text will never change. (But might have a performace hit if setting values for a large number of cells).

Using the CellToolTipTextNeeded event is also possible. To use this you should set a DataSource for the grid or set the Virtual property true. The code sample below shows how to use this.

private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
    e.ToolTipText = string.Format("This cell is in row {0}, col {1}", e.RowIndex, e.ColumnIndex);
}

However, I personally do not like the way the DataGridView places the tooltip right over the cell. So I use a ToolTip object instead. Your code is fine but needs a little adjustment.

First, put the ToolTip at form scope. This is to ensure the ToolTip object is not disposed when the CellMouseEnter event exits.

Add a panel to your form and drag/drop your datagrid in to it. Set Dock on the datagrid to Fill and position the panel how you like. (This is because the X and Y on ToolTip.Show are relative to the top left corner of the "window" object passed to it and we need to use an object otherthan the DataGridView because that hides our tooltips.)

You also need to get the X and Y values for the cell so you can place the tip properly.

ToolTip tooltip1 = new ToolTip(); // <-- ToolTip at form scope

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    if ((e.RowIndex != -1) && (e.ColumnIndex == 1))
    {
        //MessageBox.Show("TEST");
        // get x and y position of cell
        Rectangle rec = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
        Point p = rec.Location;
        // add offset to move tip up
        p.Offset(0, -(20));
        // changed dataGridView1 to panel1, use offset cell position, and timeout
        tooltip1.Show("hello", panel1, p, 2500); // show tool tip
    }
}

That's quite a lot of messing about and you might prefer the simpler first option.
Let me know how you get on.

so what about this panel1 do I create it or what? I mean I tired both methods and I still have no success.

dataGridView1.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(dataGridView1_CellToolTipTextNeeded);

I mean I am adding the events right as you can see above. I am just so confused why this won't work. I wonder what I am doing wrong

Please re-read my last post.

Add a panel to your form and drag/drop your datagrid in to it. Set Dock on the datagrid to Fill and position the panel how you like. (This is because the X and Y on ToolTip.Show are relative to the top left corner of the "window" object passed to it and we need to use an object otherthan the DataGridView because that hides our tooltips.)

well I tried what u said but I actually came up with another way todo this (as for some reason those won't work)

Actualyl I used kind of what u said and then I just realized I was doing it all wrong. I just added this line in the for loop for filling the cells you see in my original code

ataGridView1 [1, i].ToolTipText = employeeTempArray [i].isAge();

Works fine (if only I can find a way to make it appear sooner)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.