DataGridViewCell 類別

定義

表示 DataGridView 控制項中的個別儲存格。

public ref class DataGridViewCell abstract : System::Windows::Forms::DataGridViewElement, ICloneable, IDisposable
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.DataGridViewCellConverter))]
public abstract class DataGridViewCell : System.Windows.Forms.DataGridViewElement, ICloneable, IDisposable
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.DataGridViewCellConverter))>]
type DataGridViewCell = class
    inherit DataGridViewElement
    interface ICloneable
    interface IDisposable
Public MustInherit Class DataGridViewCell
Inherits DataGridViewElement
Implements ICloneable, IDisposable
繼承
DataGridViewCell
衍生
屬性
實作

範例

下列程式碼範例說明如何使用此類型。 如需此範例的詳細資訊,請參閱How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control

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

備註

類別 DataGridViewCell 代表 控制項中的 DataGridView 個別儲存格。 您可以透過 的 DataGridViewRow 集合擷 Cells 取儲存格。

的資料列和資料行 DataGridViewCell 會識別 中 DataGridView 儲存格的位置。 一般資料格包含資料列和資料行的特定資料,其中包含它所在的資料行。 您可以透過設定儲存格的屬性和處理與其相關聯的事件,以數種方式自訂資料格。

給實施者的注意事項

當您衍生自 DataGridViewCell 並將新屬性新增至衍生類別時,請務必覆寫 Clone() 方法,以在複製作業期間複製新屬性。 您也應該呼叫基類的 Clone() 方法,以便基類的屬性複製到新的儲存格。

建構函式

DataGridViewCell()

初始化 DataGridViewCell 類別的新執行個體。

屬性

AccessibilityObject

取得指定給 DataGridViewCell.DataGridViewCellAccessibleObjectDataGridViewCell

ColumnIndex

取得這個儲存格的資料行索引。

ContentBounds

取得圍住儲存格內容區域的周框。

ContextMenuStrip

取得或設定與儲存格相關聯的捷徑功能表。

DataGridView

取得與這個項目有關聯的 DataGridView 控制項。

(繼承來源 DataGridViewElement)
DefaultNewRowValue

取得新資料錄之資料列中儲存格的預設值。

Displayed

取得值,該值表示儲存格目前是否顯示於螢幕上。

EditedFormattedValue

取得目前已格式化的儲存格值,不管儲存格是否處於編輯模式,而值是否尚未認可。

EditType

取得儲存格的裝載編輯控制項型別。

ErrorIconBounds

取得儲存格之錯誤圖示的界限。

ErrorText

取得或設定文字,描述與儲存格相關聯的錯誤狀況。

FormattedValue

取得儲存格的格式化值,以供顯示。

FormattedValueType

取得與儲存格相關聯之格式化值的類型。

Frozen

取得指出是否已凍結此儲存格的值。

HasStyle

取得指出是否已經設定 Style 屬性的值。

InheritedState

取得儲存格的目前狀態,是繼承其資料列和資料行的狀態。

InheritedStyle

取得目前套用至儲存格的樣式。

IsInEditMode

取得指出目前是否正在編輯這個儲存格的值。

OwningColumn

取得包含這個儲存格的資料行。

OwningRow

取得包含這個儲存格的資料列。

PreferredSize

取得能夠容納儲存格的矩形區域大小 (以像素為單位)。

ReadOnly

取得或設定值,指出是否可以編輯儲存格的資料。

Resizable

取得指出儲存格是否可以調整大小的值。

RowIndex

取得儲存格的父資料列索引。

Selected

取得或設定值,指出是否已選取此儲存格。

Size

取得儲存格的大小。

State

取得此項目的使用者介面 (UI) 狀態。

(繼承來源 DataGridViewElement)
Style

取得或設定儲存格的樣式。

Tag

取得或設定物件,其中包含儲存格的相關補充資料。

ToolTipText

取得或設定與這個儲存格相關聯的工具提示文字。

Value

取得或設定與這個儲存格相關聯的值。

ValueType

取得或設定儲存格中值的資料類型。

Visible

取得指出儲存格是在已隱藏資料列或是資料行中的值。

方法

AdjustCellBorderStyle(DataGridViewAdvancedBorderStyle, DataGridViewAdvancedBorderStyle, Boolean, Boolean, Boolean, Boolean)

根據指定的準則,修改輸入儲存格框線樣式。

BorderWidths(DataGridViewAdvancedBorderStyle)

傳回 Rectangle,表示所有儲存格邊界的寬度。

ClickUnsharesRow(DataGridViewCellEventArgs)

指出當按下儲存格時,儲存格的資料列是否會取消共用。

Clone()

建立與這個儲存格完全相同的複本。

ContentClickUnsharesRow(DataGridViewCellEventArgs)

指出當按下儲存格內容時,儲存格的資料列是否會取消共用。

ContentDoubleClickUnsharesRow(DataGridViewCellEventArgs)

指出當按兩下儲存格內容時,儲存格的資料列是否會取消共用。

CreateAccessibilityInstance()

DataGridViewCell 建立新的可存取物件。

DetachEditingControl()

DataGridView 中移除儲存格的編輯控制項。

Dispose()

釋放 DataGridViewCell 所使用的所有資源。

Dispose(Boolean)

釋放 DataGridViewCell 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

DoubleClickUnsharesRow(DataGridViewCellEventArgs)

指出當按兩下儲存格時,儲存格的資料列是否會取消共用。

EnterUnsharesRow(Int32, Boolean)

指出當焦點移至儲存格時,是否將取消共用父資料列。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Finalize()

在記憶體回收 DataGridViewCell 之前釋放 Unmanaged 資源並執行其他的清除作業。

GetClipboardContent(Int32, Boolean, Boolean, Boolean, Boolean, String)

擷取儲存格的已格式化的值,以複製到 Clipboard

GetContentBounds(Graphics, DataGridViewCellStyle, Int32)

傳回圍住儲存格內容區域的週框,這個內容區域是使用指定的 Graphics 和儲存格樣式所計算。

GetContentBounds(Int32)

使用預設的 Graphics 和目前儲存格中所採用的儲存格樣式,傳回圍住儲存格內容區域的周框。

GetEditedFormattedValue(Int32, DataGridViewDataErrorContexts)

傳回目前已格式化的儲存格值,不管儲存格是否處於編輯模式,而值是否尚未認可。

GetErrorIconBounds(Graphics, DataGridViewCellStyle, Int32)

傳回圍住儲存格的錯誤圖示之週框 (如果有此圖示)。

GetErrorText(Int32)

傳回表示儲存格錯誤的字串。

GetFormattedValue(Object, Int32, DataGridViewCellStyle, TypeConverter, TypeConverter, DataGridViewDataErrorContexts)

取得儲存格的格式化值,以供顯示。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetInheritedContextMenuStrip(Int32)

取得目前儲存格的繼承捷徑功能表。

GetInheritedState(Int32)

傳回值,指出儲存格的目前狀態是從其資料列和資料行的狀態繼承而來。

GetInheritedStyle(DataGridViewCellStyle, Int32, Boolean)

取得套用到儲存格的樣式。

GetPreferredSize(Graphics, DataGridViewCellStyle, Int32, Size)

計算儲存格的慣用大小 (以像素為單位)。

GetSize(Int32)

取得儲存格的大小。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetValue(Int32)

取得儲存格的值。

InitializeEditingControl(Int32, Object, DataGridViewCellStyle)

初始化用來編輯儲存格的控制項。

KeyDownUnsharesRow(KeyEventArgs, Int32)

指出當焦點在儲存格上,而使用者按下按鍵時,是否取消共用父資料列。

KeyEntersEditMode(KeyEventArgs)

判斷是否應該根據指定的按鍵啟動編輯模式。

KeyPressUnsharesRow(KeyPressEventArgs, Int32)

指出如果當焦點在資料列中的儲存格時按下按鍵,是否會取消資料列的共用。

KeyUpUnsharesRow(KeyEventArgs, Int32)

指出當焦點在儲存格上,而使用者放開按鍵時,是否會取消父資料列的共用。

LeaveUnsharesRow(Int32, Boolean)

指出當焦點移出資料列中的儲存格時,是否取消共用該資料列。

MeasureTextHeight(Graphics, String, Font, Int32, TextFormatFlags)

在指定的特性下,取得所指定文字的高度 (以像素為單位)。

MeasureTextHeight(Graphics, String, Font, Int32, TextFormatFlags, Boolean)

在指定的特性下,取得所指定文字的高度 (以像素為單位)。 而且也指出所需要的寬度是否大於指定的最大寬度。

MeasureTextPreferredSize(Graphics, String, Font, Single, TextFormatFlags)

在指定的特性下,取得所指定文字的理想高度和寬度。

MeasureTextSize(Graphics, String, Font, TextFormatFlags)

在指定的特性下,取得所指定文字的高度和寬度。

MeasureTextWidth(Graphics, String, Font, Int32, TextFormatFlags)

在指定的特性下,取得所指定文字的寬度 (以像素為單位)。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MouseClickUnsharesRow(DataGridViewCellMouseEventArgs)

指出當指標在資料列的儲存格上,而使用者按一下滑鼠按鈕時,該資料列是否取消共用。

MouseDoubleClickUnsharesRow(DataGridViewCellMouseEventArgs)

指出當使用者按兩下資料列中的儲存格時,是否取消共用該資料列。

MouseDownUnsharesRow(DataGridViewCellMouseEventArgs)

指出當指標在資料列的儲存格上,而按住滑鼠按鈕時,該資料列是否取消共用。

MouseEnterUnsharesRow(Int32)

指出當滑鼠指標移到某資料列的儲存格上方時,是否取消共用該資料列。

MouseLeaveUnsharesRow(Int32)

指示當滑鼠指標離開某列時,該列是否共用。

MouseMoveUnsharesRow(DataGridViewCellMouseEventArgs)

指出當滑鼠指標移到某資料列的儲存格上方時,是否取消共用該資料列。

MouseUpUnsharesRow(DataGridViewCellMouseEventArgs)

指出當指標在資料列的儲存格上,而使用者釋放滑鼠按鈕時,該資料列是否取消共用。

OnClick(DataGridViewCellEventArgs)

當按下儲存格時呼叫。

OnContentClick(DataGridViewCellEventArgs)

當按下儲存格內容時呼叫。

OnContentDoubleClick(DataGridViewCellEventArgs)

當按兩下儲存格內容時呼叫。

OnDataGridViewChanged()

當儲存格的 DataGridView 屬性變更時呼叫。

OnDoubleClick(DataGridViewCellEventArgs)

當按兩下儲存格時呼叫。

OnEnter(Int32, Boolean)

當焦點移至儲存格時呼叫。

OnKeyDown(KeyEventArgs, Int32)

當焦點在儲存格上,按下字元鍵時即會呼叫。

OnKeyPress(KeyPressEventArgs, Int32)

當焦點在儲存格上,按下按鍵時呼叫。

OnKeyUp(KeyEventArgs, Int32)

當焦點在儲存格上,放開字元鍵時呼叫。

OnLeave(Int32, Boolean)

當焦點從儲存格移動時呼叫。

OnMouseClick(DataGridViewCellMouseEventArgs)

當滑鼠指標在儲存格上,使用者按一下滑鼠按鈕時呼叫。

OnMouseDoubleClick(DataGridViewCellMouseEventArgs)

當滑鼠指標在儲存格上,使用者按兩下滑鼠按鈕時呼叫。

OnMouseDown(DataGridViewCellMouseEventArgs)

當滑鼠指標在儲存格上,使用者按住滑鼠按鈕時呼叫。

OnMouseEnter(Int32)

當滑鼠指標移到儲存格上方時呼叫。

OnMouseLeave(Int32)

當滑鼠指標離開儲存格時呼叫。

OnMouseMove(DataGridViewCellMouseEventArgs)

當滑鼠指標移到儲存格內時呼叫。

OnMouseUp(DataGridViewCellMouseEventArgs)

當滑鼠指標在儲存格上,使用者放開滑鼠按鈕時呼叫。

Paint(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts)

繪製目前的 DataGridViewCell

PaintBorder(Graphics, Rectangle, Rectangle, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle)

繪製目前 DataGridViewCell 的框線。

PaintErrorIcon(Graphics, Rectangle, Rectangle, String)

繪製目前 DataGridViewCell 的錯誤圖示。

ParseFormattedValue(Object, DataGridViewCellStyle, TypeConverter, TypeConverter)

將為了顯示而格式化的值轉換成實際的儲存格值。

PositionEditingControl(Boolean, Boolean, Rectangle, Rectangle, DataGridViewCellStyle, Boolean, Boolean, Boolean, Boolean)

設定由 DataGridView 控制項中的儲存格裝載之編輯控制項的位置和大小。

PositionEditingPanel(Rectangle, Rectangle, DataGridViewCellStyle, Boolean, Boolean, Boolean, Boolean)

設定由儲存格裝載之編輯面板的位置和大小,並傳回編輯面板中編輯控制項的標準界限。

RaiseCellClick(DataGridViewCellEventArgs)

引發 CellClick 事件。

(繼承來源 DataGridViewElement)
RaiseCellContentClick(DataGridViewCellEventArgs)

引發 CellContentClick 事件。

(繼承來源 DataGridViewElement)
RaiseCellContentDoubleClick(DataGridViewCellEventArgs)

引發 CellContentDoubleClick 事件。

(繼承來源 DataGridViewElement)
RaiseCellValueChanged(DataGridViewCellEventArgs)

引發 CellValueChanged 事件。

(繼承來源 DataGridViewElement)
RaiseDataError(DataGridViewDataErrorEventArgs)

引發 DataError 事件。

(繼承來源 DataGridViewElement)
RaiseMouseWheel(MouseEventArgs)

引發 MouseWheel 事件。

(繼承來源 DataGridViewElement)
SetValue(Int32, Object)

設定儲存格的值。

ToString()

傳回描述目前物件的字串。

適用於

另請參閱