Redigera

Dela via


DataGridViewCell.ToolTipText Property

Definition

Gets or sets the ToolTip text associated with this cell.

public:
 property System::String ^ ToolTipText { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public string ToolTipText { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ToolTipText : string with get, set
Public Property ToolTipText As String

Property Value

The ToolTip text associated with the cell. The default is Empty.

Attributes

Examples

The following code example shows how to set the ToolTipText property within an event handler for the CellFormatting event. This example is part of a larger code example provided in How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control.

// 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";
        }
    }
}
// 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

Remarks

The value of this property is displayed as the cell ToolTip when the mouse pointer is over the cell and the property value is not Empty. If the value of this property is Empty, the cell will display a ToolTip containing the value of the cell if the value is truncated in the cell display; otherwise, the cell will not display a ToolTip. You can also prevent the display of a ToolTip by setting the DataGridView.ShowCellToolTips property to false.

When the DataGridView control DataSource property is set or its VirtualMode property is true, getting the value of the ToolTipText property raises the CellToolTipTextNeeded event of the control and returns the value of the DataGridViewCellToolTipTextNeededEventArgs.ToolTipText property as specified in the event handler. If there are no handlers for the event, getting the value of the ToolTipText property returns the previously specified value or its default value of Empty.

Handling the CellToolTipTextNeeded event is primarily useful when working with large amounts of data to avoid performance penalties when setting the cell ToolTipText value for multiple cells. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Changing this property raises the CellToolTipTextChanged event on the owning DataGridView, if one exists.

Applies to

See also