DataGridViewCell.Value 屬性

定義

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

public:
 property System::Object ^ Value { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Browsable(false)]
public object Value { get; set; }
[System.ComponentModel.Browsable(false)]
public object? Value { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.Value : obj with get, set
Public Property Value As Object

屬性值

取得或設定要由儲存格顯示的資料。 預設為 null

屬性

例外狀況

RowIndex 不在有效範圍內,亦即從 0 到控制項中資料列數減 1 的範圍。

ColumnIndex 小於 0,表示儲存格是資料列行首儲存格。

範例

下列程式碼範例示範如何使用 屬性來更新儲存格的內容 Value 。 此範例是如何:操作 DataGridView 控制項中 Windows Forms資料列中所提供較大程式碼範例的一部分。

// Give cheescake excellent rating.
void Button8_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   UpdateStars( dataGridView->Rows[ 4 ], L"******************" );
}

int ratingColumn;
void UpdateStars( DataGridViewRow^ row, String^ stars )
{
   row->Cells[ ratingColumn ]->Value = stars;
   
   // Resize the column width to account for the new value.
   row->DataGridView->AutoResizeColumn( ratingColumn, DataGridViewAutoSizeColumnMode::DisplayedCells );
}
// Give cheescake excellent rating.
private void Button8_Click(object sender,
    System.EventArgs e)
{
    UpdateStars(dataGridView.Rows[4], "******************");
}

int ratingColumn = 3;

private void UpdateStars(DataGridViewRow row, string stars)
{

    row.Cells[ratingColumn].Value = stars;

    // Resize the column width to account for the new value.
    row.DataGridView.AutoResizeColumn(ratingColumn, 
        DataGridViewAutoSizeColumnMode.DisplayedCells);
}
' Give cheescake excellent rating.
Private Sub Button8_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button8.Click

    UpdateStars(dataGridView.Rows(4), "******************")
End Sub

Private ratingColumn As Integer = 3

Private Sub UpdateStars(ByVal row As DataGridViewRow, _
    ByVal stars As String)

    row.Cells(ratingColumn).Value = stars

    ' Resize the column width to account for the new value.
    row.DataGridView.AutoResizeColumn(ratingColumn, _
        DataGridViewAutoSizeColumnMode.DisplayedCells)

End Sub

備註

當您將不同的值指派給儲存格時, CellValueChanged 會引發 控制項的事件 DataGridView

屬性 Value 是資料格所包含的實際資料物件,而 FormattedValue 屬性則是資料的格式化表示。 ValueTypeFormattedValueType 屬性分別對應至這些值的資料類型。

當您設定 Value 屬性時,指定的值不會自動從格式化、顯示值轉換成基礎儲存格值。 例如, DataGridViewCellStyle 會忽略儲存格的效果,因此將 設定 ValueDataGridViewCellStyle.NullValue 不會產生 屬性值 DataGridViewCellStyle.DataSourceNullValue

適用於

另請參閱