DataGridViewCellToolTipTextNeededEventHandler 委托

定义

表示将用来处理 CellToolTipTextNeededDataGridView 事件的方法。

public delegate void DataGridViewCellToolTipTextNeededEventHandler(System::Object ^ sender, DataGridViewCellToolTipTextNeededEventArgs ^ e);
public delegate void DataGridViewCellToolTipTextNeededEventHandler(object sender, DataGridViewCellToolTipTextNeededEventArgs e);
public delegate void DataGridViewCellToolTipTextNeededEventHandler(object? sender, DataGridViewCellToolTipTextNeededEventArgs e);
type DataGridViewCellToolTipTextNeededEventHandler = delegate of obj * DataGridViewCellToolTipTextNeededEventArgs -> unit
Public Delegate Sub DataGridViewCellToolTipTextNeededEventHandler(sender As Object, e As DataGridViewCellToolTipTextNeededEventArgs)

参数

sender
Object

事件源。

示例

下面的代码示例使用工具提示显示有关列中单元格内容 ReportsTo 的其他信息。

DataTable^ Populate( String^ query, bool resetUnsharedCounter )
{
    if ( resetUnsharedCounter )
    {
        ResetCounter();
    }


    // Alter the data source as necessary
    SqlDataAdapter^ adapter = gcnew SqlDataAdapter( query,
        gcnew SqlConnection( L"Integrated Security=SSPI;Persist Security Info=False;"
        L"Initial Catalog=Northwind;Data Source= localhost" ) );
    DataTable^ table = gcnew DataTable;
    adapter->Fill( table );
    return table;
}

Label^ count;
int unsharedRowCounter;
void ResetCounter()
{
    unsharedRowCounter = 0;
    count->Text = unsharedRowCounter.ToString();
}

void dataGridView1_CellToolTipTextNeeded( Object^ /*sender*/,
    DataGridViewCellToolTipTextNeededEventArgs^ e )
{
    if ( theCellImHoveringOver->ColumnIndex == dataGridView1->Columns[ L"ReportsTo" ]->Index && theCellImHoveringOver->RowIndex > -1 )
    {
        String^ reportsTo = dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value->ToString();
        if ( reportsTo->Equals( L"" ) )
        {
            e->ToolTipText = L"The buck stops here!";
        }
        else
        {
            DataTable^ table = Populate( String::Format( L"select firstname, lastname from employees where employeeid = '{0}'",
                dataGridView1->Rows[ theCellImHoveringOver->RowIndex ]->Cells[ theCellImHoveringOver->ColumnIndex ]->Value ), false );
            e->ToolTipText = String::Format( L"Reports to {0} {1}", table->Rows[ 0 ]->ItemArray[ 0 ], table->Rows[ 0 ]->ItemArray[ 1 ] );
        }
    }
}
private DataTable Populate(string query, bool resetUnsharedCounter)
{
    if (resetUnsharedCounter)
    {
        ResetCounter();
    }

    // Alter the data source as necessary
    SqlDataAdapter adapter = new SqlDataAdapter(query,
        new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
        "Initial Catalog=Northwind;Data Source=localhost"));

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);
    return table;
}

private Label count = new Label();
private int unsharedRowCounter;

private void ResetCounter()
{
    unsharedRowCounter = 0;
    count.Text = unsharedRowCounter.ToString();
}

private void dataGridView1_CellToolTipTextNeeded(object sender,
    DataGridViewCellToolTipTextNeededEventArgs e)
{

    if (theCellImHoveringOver.ColumnIndex == dataGridView1.Columns["ReportsTo"].Index &&
        theCellImHoveringOver.RowIndex > -1)
    {

        string reportsTo = dataGridView1.Rows[theCellImHoveringOver.RowIndex].
            Cells[theCellImHoveringOver.ColumnIndex].Value.ToString();

        if (String.IsNullOrEmpty(reportsTo))
        {
            e.ToolTipText = "The buck stops here!";
        }
        else
        {
            DataTable table = Populate(
                "select firstname, lastname from employees where employeeid = '" +
                dataGridView1.Rows[theCellImHoveringOver.RowIndex].
                Cells[theCellImHoveringOver.ColumnIndex].Value.ToString() +
                "'", false);

            e.ToolTipText = "Reports to " + table.Rows[0].ItemArray[0] + " " +
                table.Rows[0].ItemArray[1];
        }
    }
}
Private Function Populate(ByVal query As String, ByVal resetUnsharedCounter As Boolean) As DataTable

    If resetUnsharedCounter Then
        ResetCounter()
    End If

    ' Alter the data source as necessary
    Dim adapter As New SqlDataAdapter(query, _
        New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" & _
        "Initial Catalog=Northwind;Data Source=localhost"))

    Dim table As New DataTable()
    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    adapter.Fill(table)
    Return table
End Function

Private count As New Label()
Private unsharedRowCounter As Integer

Private Sub ResetCounter()
    unsharedRowCounter = 0
    count.Text = unsharedRowCounter.ToString()
End Sub

Private Sub DataGridView1_CellToolTipTextNeeded(ByVal sender As Object, _
    ByVal e As DataGridViewCellToolTipTextNeededEventArgs) _
    Handles dataGridView1.CellToolTipTextNeeded

    If theCellImHoveringOver.ColumnIndex = dataGridView1.Columns("ReportsTo").Index AndAlso _
            theCellImHoveringOver.RowIndex > -1 Then

        Dim reportsTo As String = dataGridView1.Rows(theCellImHoveringOver.RowIndex). _
            Cells(theCellImHoveringOver.ColumnIndex).Value.ToString()
        If String.IsNullOrEmpty(reportsTo) Then
            e.ToolTipText = "The buck stops here!"
        Else
            Dim table As DataTable = Populate( _
                "select firstname, lastname from employees where employeeid = '" & _
                dataGridView1.Rows(theCellImHoveringOver.RowIndex). _
                Cells(theCellImHoveringOver.ColumnIndex).Value.ToString() & _
                "'", False)

            e.ToolTipText = "Reports to " & table.Rows(0).Item(0).ToString() & " " & table.Rows(0).Item(1).ToString()
        End If
    End If
End Sub

注解

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

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

每当 CellToolTipTextNeeded 以编程方式检索属性的值 DataGridViewCell.ToolTipText 或鼠标指针进入单元格时,也会发生该事件。

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

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

有关如何处理事件的详细信息,请参阅 处理和引发事件

创建 DataGridViewCellToolTipTextNeededEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

另请参阅