DataGridView.CellToolTipTextNeeded 事件

定义

需要单元格的工具提示文本时发生。

C#
public event System.Windows.Forms.DataGridViewCellToolTipTextNeededEventHandler CellToolTipTextNeeded;
C#
public event System.Windows.Forms.DataGridViewCellToolTipTextNeededEventHandler? CellToolTipTextNeeded;

事件类型

示例

下面的代码示例演示如何使用 CellToolTipTextNeeded 事件处理程序显示数据绑定 DataGridView 控件中隐藏列的信息。

C#
void dataGridView1_CellToolTipTextNeeded(object sender,
    DataGridViewCellToolTipTextNeededEventArgs e)
{
    string newLine = Environment.NewLine;
    if (e.RowIndex > -1)
    {
        DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];

        // Add the employee's ID to the ToolTipText.
        e.ToolTipText = String.Format("EmployeeID {0}:{1}",
            dataGridViewRow1.Cells["EmployeeID"].Value, newLine);

        // Add the employee's name to the ToolTipText.
        e.ToolTipText += String.Format("{0} {1} {2}{3}",
            dataGridViewRow1.Cells["TitleOfCourtesy"].Value.ToString(),
            dataGridViewRow1.Cells["FirstName"].Value.ToString(),
            dataGridViewRow1.Cells["LastName"].Value.ToString(),
            newLine);

        // Add the employee's title to the ToolTipText.
        e.ToolTipText += String.Format("{0}{1}{2}",
            dataGridViewRow1.Cells["Title"].Value.ToString(),
            newLine, newLine);

        // Add the employee's contact information to the ToolTipText.
        e.ToolTipText += String.Format("{0}{1}{2}, ",
            dataGridViewRow1.Cells["Address"].Value.ToString(), newLine,
            dataGridViewRow1.Cells["City"].Value.ToString());
        if (!String.IsNullOrEmpty(
            dataGridViewRow1.Cells["Region"].Value.ToString()))
        {
            e.ToolTipText += String.Format("{0}, ",
                dataGridViewRow1.Cells["Region"].Value.ToString());
        }
        e.ToolTipText += String.Format("{0}, {1}{2}{3} EXT:{4}{5}{6}",
            dataGridViewRow1.Cells["Country"].Value.ToString(),
            dataGridViewRow1.Cells["PostalCode"].Value.ToString(),
            newLine, dataGridViewRow1.Cells["HomePhone"].Value.ToString(),
            dataGridViewRow1.Cells["Extension"].Value.ToString(),
            newLine, newLine);

        // Add employee information to the ToolTipText.
        DateTime HireDate =
            (DateTime)dataGridViewRow1.Cells["HireDate"].Value;
        e.ToolTipText +=
            String.Format("Employee since: {0}/{1}/{2}{3}Manager: {4}",
            HireDate.Month.ToString(), HireDate.Day.ToString(),
            HireDate.Year.ToString(), newLine,
            dataGridViewRow1.Cells["Manager"].Value.ToString());
    }
}

注解

CellToolTipTextNeeded仅当控件属性已设置或其VirtualMode属性为 trueDataGridViewDataSource,才会发生该事件。

处理 CellToolTipTextNeeded 事件时,如果 ShowCellToolTips 属性值为 true 且鼠标指针位于单元格上方,或者用户使用键盘导航到单元格,则会显示处理程序中指定的工具提示文本。 CellToolTipTextNeeded如果要显示由单元格的当前状态或值确定的工具提示,则 事件非常有用。

CellToolTipTextNeeded每当以编程方式检索属性的值DataGridViewCell.ToolTipText时,或者当用户使用鼠标或键盘导航到单元格时,也会发生该事件。

可以使用 DataGridViewCellEventArgs.ColumnIndexRowIndex 属性来确定单元格的状态或值,并使用此信息更改或修改 DataGridViewCellToolTipTextNeededEventArgs.ToolTipText 属性。 此属性使用事件值替代的单元格 ToolTipText 属性的值进行初始化。

CellToolTipTextNeeded处理大量数据时处理 事件,以避免为多个单元格设置单元格ToolTipText值而造成性能损失。 有关详细信息,请参阅 缩放 Windows 窗体 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

另请参阅