DataGridViewCell.ToolTipText DataGridViewCell.ToolTipText DataGridViewCell.ToolTipText DataGridViewCell.ToolTipText Property

定义

获取或设置与此单元格关联的 ToolTip 文本。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; }
member this.ToolTipText : string with get, set
Public Property ToolTipText As String

属性值

与该单元格关联的工具提示文本。The ToolTip text associated with the cell. 默认值为 EmptyThe default is Empty.

示例

下面的代码示例演示如何设置ToolTipText内的事件处理程序属性CellFormatting事件。The following code example shows how to set the ToolTipText property within an event handler for the CellFormatting event. 此示例中提供一个更大的代码示例摘自如何:在 Windows 中的单个单元格工具提示窗体 DataGridView 控件添加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 'dataGridView1_CellFormatting

注解

当鼠标指针位于单元格上且属性值不是,此属性的值显示为工具提示的单元格EmptyThe 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. 如果此属性的值为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. 您还可以通过设置阻止的工具提示显示DataGridView.ShowCellToolTips属性设置为falseYou can also prevent the display of a ToolTip by setting the DataGridView.ShowCellToolTips property to false.

DataGridView控件DataSource属性设置或其VirtualMode属性是true,获取的值ToolTipText属性将引发CellToolTipTextNeeded控件的事件,并返回的值DataGridViewCellToolTipTextNeededEventArgs.ToolTipText为属性指定在事件处理程序。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. 如果没有为事件处理程序,则获取的值ToolTipText属性返回以前指定的值或其默认值为EmptyIf 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.

处理CellToolTipTextNeeded事件时,可以主要使用大量的数据以避免性能损失,设置单元格时ToolTipText多个单元格的值。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. 有关详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

更改此属性将引发CellToolTipTextChanged对所具有的事件DataGridView,如果存在一个。Changing this property raises the CellToolTipTextChanged event on the owning DataGridView, if one exists.

适用于

另请参阅