Export (0) Print
Expand All

How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control

By default, ToolTips are used to display the values of DataGridView cells that are too small to show their entire contents. You can override this behavior, however, to set ToolTip-text values for individual cells. This is useful to display to users additional information about a cell or to provide to users an alternate description of the cell contents. For example, if you have a row that displays status icons, you may want to provide text explanations using ToolTips.

You can also disable the display of cell-level ToolTips by setting the DataGridView.ShowCellToolTips property to false.

To add a ToolTip to a cell

  • Set the DataGridViewCell.ToolTipText property.

    
    // Sets the ToolTip text for cells in the Rating column.
    void dataGridView1_CellFormatting(object sender, 
        DataGridViewCellFormattingEventArgs e)
    {
        if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
            && e.Value != null )
        {
            DataGridViewCell cell = 
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (e.Value.Equals("*"))
            {                
                cell.ToolTipText = "very bad";
            }
            else if (e.Value.Equals("**"))
            {
                cell.ToolTipText = "bad";
            }
            else if (e.Value.Equals("***"))
            {
                cell.ToolTipText = "good";
            }
            else if (e.Value.Equals("****"))
            {
                cell.ToolTipText = "very good";
            }
        }
    }
    
    
    

  • This example requires:

  • A DataGridView control named dataGridView1 that contains a column named Rating for displaying string values of one through four asterisk ("*") symbols. The CellFormatting event of the control must be associated with the event handler method shown in the example.

  • References to the System and System.Windows.Forms assemblies.

When you bind the DataGridView control to an external data source or provide your own data source by implementing virtual mode, you might encounter performance issues. To avoid a performance penalty when working with large amounts of data, handle the CellToolTipTextNeeded event rather than setting the ToolTipText property of multiple cells. When you handle this event, getting the value of a cell ToolTipText property raises the event and returns the value of the DataGridViewCellToolTipTextNeededEventArgs.ToolTipText property as specified in the event handler.

Community Additions

ADD
Show:
© 2014 Microsoft