DataGridViewCell.ValueType DataGridViewCell.ValueType DataGridViewCell.ValueType DataGridViewCell.ValueType Property


取得或設定儲存格中值的資料型別。Gets or sets the data type of the values in the cell.

 virtual property Type ^ ValueType { Type ^ get(); void set(Type ^ value); };
public virtual Type ValueType { get; set; }
member this.ValueType : Type with get, set
Public Overridable Property ValueType As Type


Type,表示儲存格中值的資料型別。A Type representing the data type of the value in the cell.


下列程式碼範例示範如何使用這個屬性。The following code example demonstrates the use of this property.

private DataGridView dataGridView1 = new DataGridView();

private void AddColorColumn()
    DataGridViewComboBoxColumn comboBoxColumn =
        new DataGridViewComboBoxColumn();
        Color.Red, Color.Yellow, Color.Green, Color.Blue);
    comboBoxColumn.ValueType = typeof(Color);
    dataGridView1.EditingControlShowing +=
        new DataGridViewEditingControlShowingEventHandler(

private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
    ComboBox combo = e.Control as ComboBox;
    if (combo != null)
        // Remove an existing event-handler, if present, to avoid 
        // adding multiple handlers when the editing control is reused.
        combo.SelectedIndexChanged -=
            new EventHandler(ComboBox_SelectedIndexChanged);

        // Add the event handler. 
        combo.SelectedIndexChanged +=
            new EventHandler(ComboBox_SelectedIndexChanged);

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
Private WithEvents dataGridView1 As New DataGridView()

Private Sub AddColorColumn()

    Dim comboBoxColumn As New DataGridViewComboBoxColumn()
    comboBoxColumn.Items.AddRange( _
        Color.Red, Color.Yellow, Color.Green, Color.Blue)
    comboBoxColumn.ValueType = GetType(Color)

End Sub

Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, _
    ByVal e As DataGridViewEditingControlShowingEventArgs) _
    Handles dataGridView1.EditingControlShowing

    Dim combo As ComboBox = CType(e.Control, ComboBox)
    If (combo IsNot Nothing) Then

        ' Remove an existing event-handler, if present, to avoid 
        ' adding multiple handlers when the editing control is reused.
        RemoveHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf ComboBox_SelectedIndexChanged)

        ' Add the event handler. 
        AddHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf ComboBox_SelectedIndexChanged)

    End If

End Sub

Private Sub ComboBox_SelectedIndexChanged( _
    ByVal sender As Object, ByVal e As EventArgs)

    Dim comboBox1 As ComboBox = CType(sender, ComboBox)
    comboBox1.BackColor = _
        CType(CType(sender, ComboBox).SelectedItem, Color)

End Sub


如果尚未ValueType設定儲存格的屬性,就會使用主控DataGridViewColumn.ValueType資料行的屬性(如果有的話)。If the ValueType property for the cell has not been set, the DataGridViewColumn.ValueType property for the owning column is used, if it exists.

屬性是儲存格所包含的實際資料物件, FormattedValue而屬性則是格式化的資料標記法。 ValueThe Value property is the actual data object contained by the cell, whereas the FormattedValue property is the formatted representation of the data. ValueTypeFormattedValueType屬性分別對應至這些值的資料類型。The ValueType and FormattedValueType properties correspond to the data types of these values, respectively.