共用方式為


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 屬性更新儲存格的內容。 此範例是《 如何操作 Windows Forms DataGridView 控制項中的列》中提供的完整程式碼範例的一部分。

// 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 該格的 in 效果被忽略,因此設 ValueDataGridViewCellStyle.NullValue 不會導致屬性值為 DataGridViewCellStyle.DataSourceNullValue

適用於

另請參閱