DataGridViewCell.ToolTipText 属性

定义

获取或设置与此单元格关联的 ToolTip 文本。

C#
[System.ComponentModel.Browsable(false)]
public string ToolTipText { get; set; }

属性值

与该单元格关联的工具提示文本。 默认值为 Empty

属性

示例

下面的代码示例演示如何在 CellFormatting 事件的事件处理程序中设置 ToolTipText 属性。 本示例是如何:将工具提示添加到 Windows 窗体 DataGridView 控件中的单个单元格中提供的更大代码示例的一部分。

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

注解

当鼠标指针位于单元格上且属性值不是 Empty时,此属性的值显示为单元格工具提示。 如果此属性 Empty的值为 ,则单元格将显示包含单元格值的工具提示(如果该值在单元格显示中被截断);否则,单元格将不会显示工具提示。 还可以通过将 属性设置为 DataGridView.ShowCellToolTipsfalse来防止显示工具提示。

DataGridView当设置控件DataSource属性或其 VirtualMode 属性为 true时,获取 属性的值ToolTipTextCellToolTipTextNeeded引发控件的 事件,并返回事件处理程序中指定的 属性的值DataGridViewCellToolTipTextNeededEventArgs.ToolTipText。 如果事件没有处理程序,则获取 属性的值 ToolTipText 将返回先前指定的值或其默认值 Empty

CellToolTipTextNeeded处理大量数据时,处理事件主要有用,以避免在设置多个单元格的单元格ToolTipText值时造成性能损失。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法

更改此属性将在 CellToolTipTextChanged 拥有 DataGridView的 上引发 事件(如果存在)。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅