Udostępnij za pośrednictwem


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";
                }
            }
        }
    
        ' Sets the ToolTip text for cells in the Rating column.
        Sub dataGridView1_CellFormatting(ByVal sender As Object, _
            ByVal e As DataGridViewCellFormattingEventArgs) _
            Handles dataGridView1.CellFormatting
    
            If e.ColumnIndex = Me.dataGridView1.Columns("Rating").Index _
                AndAlso (e.Value IsNot Nothing) Then
    
                With Me.dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
    
                    If e.Value.Equals("*") Then
                        .ToolTipText = "very bad"
                    ElseIf e.Value.Equals("**") Then
                        .ToolTipText = "bad"
                    ElseIf e.Value.Equals("***") Then
                        .ToolTipText = "good"
                    ElseIf e.Value.Equals("****") Then
                        .ToolTipText = "very good"
                    End If
    
                End With
    
            End If
    
        End Sub 'dataGridView1_CellFormatting
    
        // 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 != nullptr )
            {
                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";
                }
            }
        }
    

Compiling the Code

  • 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.

Robust Programming

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.

See Also

DataGridView
DataGridView.ShowCellToolTips
DataGridView.CellToolTipTextNeeded
DataGridViewCell
DataGridViewCell.ToolTipText
Programming with Cells, Rows, and Columns in the Windows Forms DataGridView Control