共用方式為


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只有在設定控件DataSource屬性或其屬性為 trueDataGridView,才會VirtualMode發生此事件。

當您處理 CellToolTipTextNeeded 事件時,每當滑鼠指標位於儲存格上方,且控件 ShowCellToolTips 屬性值為 true時,就會顯示處理程式中指定的工具提示文字。 當您想要顯示數據格目前狀態或值所決定的工具提示時,此 CellToolTipTextNeeded 事件很有用。

CellToolTipTextNeeded每當以程式設計方式擷取屬性的值DataGridViewCell.ToolTipText,或滑鼠指標進入單元格時,也會發生此事件。

您可以使用 DataGridViewCellEventArgs.ColumnIndexRowIndex 屬性來判斷儲存格的狀態或值,並使用這項資訊來設定 ToolTipText 屬性。 這個屬性會使用事件值覆寫的儲存格 ToolTipText 屬性值初始化。

CellToolTipTextNeeded處理大量數據時處理事件,以避免設定ToolTipText多個儲存格單元格值的效能負面影響。 如需詳細資訊,請參閱 縮放 Windows Form DataGridView 控制項的最佳做法

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

建立 DataGridViewCellToolTipTextNeededEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱