ValueType Property

DataGridViewColumn.ValueType Property


Gets or sets the data type of the values in the column's cells.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public Type ValueType { get; set; }

Property Value

Type: System.Type

A Type that describes the run-time class of the values stored in the column's cells.

This property is used when filtering or sorting the columns with respect to the contents of their cells.

The following code example demonstrates how to use the ValueType property in the initialization of a DataGridViewComboBoxColumn that contains Color values.

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;

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft