通过


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 超出控件中行数减 1 的有效范围 0。

ColumnIndex 小于 0,表示单元格是行标题单元格。

示例

下面的代码示例演示如何使用 Value 属性更新单元格的内容。 此示例是 操作 Windows 窗体 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 该属性是数据的格式化表示形式。 这些 ValueType 值的数据类型和 FormattedValueType 属性分别对应于这些值的数据类型。

设置 Value 属性时,指定的值不会从格式化的显示值自动转换为基础单元格值。 例如,DataGridViewCellStyle忽略单元格的效果,因此设置为ValueDataGridViewCellStyle.NullValue不会导致属性值为 DataGridViewCellStyle.DataSourceNullValue.

适用于

另请参阅