共用方式為


HOW TO:將工具提示加入至 Windows Form DataGridView 控制項中的個別儲存格

更新:2007 年 11 月

根據預設,工具提示是用來顯示太小而無法顯示出完整內容的 DataGridView 儲存格的值。不過您可以覆寫這個行為,為個別儲存格設定工具提示值。這在為使用者顯示儲存格的其他資訊,或提供使用者儲存格內容的替代描述上,是非常有用的。例如,如果您有顯示狀態圖示的資料列,可能會希望使用工具提供文字說明。

也可以藉由將 DataGridView.ShowCellToolTips 屬性為 false,停用顯示儲存格層級工具提示。

若要將工具提示加入至儲存格

  • 設定 DataGridViewCell.ToolTipText 屬性。

    ' 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
    
    // 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.
    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";
            }
        }
    }
    

編譯程式碼

  • 這項範例需要:

  • 名為 dataGridView1 的 DataGridView 控制項,其中包含名為 Rating 的資料行,顯示一到四個星號 ("*") 符號的字串值。控制項的 CellFormatting 事件必須與範例中所示的事件處理常式方法相關聯。

  • SystemSystem.Windows.Forms 組件的參考。

穩固程式設計

當您繫結 DataGridView 控制項至外部資料來源,或藉由實作虛擬模式提供自己的資料來源時,您可能會遇到效能問題。若要避免在使用大量資料時出現效能的負面效應,請處理 CellToolTipTextNeeded 事件,而非設定多個儲存格的 ToolTipText 屬性。當您處理這個事件時,取得儲存格 ToolTipText 屬性的值會引發事件,並傳回在事件處理常式中所指定的 DataGridViewCellToolTipTextNeededEventArgs.ToolTipText 屬性值。

請參閱

參考

DataGridView

DataGridView.ShowCellToolTips

DataGridView.CellToolTipTextNeeded

DataGridViewCell

DataGridViewCell.ToolTipText

其他資源

在 Windows Form DataGridView 控制項中利用儲存格、資料列和資料行進行程式設計