ThreeState Property
Collapse the table of content
Expand the table of content

DataGridViewCheckBoxColumn.ThreeState Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets a value indicating whether the hosted check box cells will allow three check states rather than two.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public bool ThreeState { get; set; }
/** @property */
public boolean get_ThreeState ()

/** @property */
public void set_ThreeState (boolean value)

public function get ThreeState () : boolean

public function set ThreeState (value : boolean)

Property Value

true if the hosted DataGridViewCheckBoxCell objects are able to have a third, indeterminate, state; otherwise, false. The default is false.

Exception typeCondition

InvalidOperationException

The value of the CellTemplate property is a null reference (Nothing in Visual Basic).

The indeterminate state can be useful, for example, when you do not want to set a default value in the check box.

Getting or setting this property gets or sets the ThreeState property of the cell object returned by the CellTemplate property. Setting this property also sets the ThreeState property of every cell in the column and refreshes the column display. To override the specified value for individual cells, set the cell values after you set the column value.

If the NullValue property of the object returned by the DefaultCellStyle property has a value of false, changing the ThreeState property value to true automatically sets NullValue to Indeterminate. If NullValue has a value of Indeterminate, changing the ThreeState property value to false automatically sets NullValue to false.

The following code example uses a DataGridViewCheckBoxColumn to track the status of office lighting. The FalseValue property associates "turnedOff" with false, the TrueValue property associates "turnedOn" with true, and the IndeterminateValue property associates "unknown" to indeterminate.

using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;

public class TriValueVirtualCheckBox:Form
{
    DataGridView dataGridView1 = new DataGridView();

    const int initialSize = 500;

    Dictionary<int, LightStatus> store 
        = new Dictionary<int, LightStatus>();

    public TriValueVirtualCheckBox() : base()
    {        
        Text = this.GetType().Name;

        int index = 0;
        for(index=0; index<=initialSize; index++)
            store.Add(index, LightStatus.Unknown);

        Controls.Add(dataGridView1);
        dataGridView1.VirtualMode = true;
        dataGridView1.AllowUserToDeleteRows = false;
        dataGridView1.CellValueNeeded += new 
            DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
        dataGridView1.CellValuePushed += new 
            DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);

        dataGridView1.Columns.Add(CreateCheckBoxColumn());
        dataGridView1.Rows.AddCopies(0, initialSize);
    }

    private DataGridViewCheckBoxColumn CreateCheckBoxColumn()
    {
        DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1 
            = new DataGridViewCheckBoxColumn();
        dataGridViewCheckBoxColumn1.HeaderText = "Lights On";
        dataGridViewCheckBoxColumn1.TrueValue = LightStatus.TurnedOn;
        dataGridViewCheckBoxColumn1.FalseValue = LightStatus.TurnedOff;
        dataGridViewCheckBoxColumn1.IndeterminateValue 
            = LightStatus.Unknown;
        dataGridViewCheckBoxColumn1.ThreeState = true;
        dataGridViewCheckBoxColumn1.ValueType = typeof(LightStatus);
        return dataGridViewCheckBoxColumn1;
    }

#region "data store maintance"
    private void dataGridView1_CellValueNeeded(object sender, 
        DataGridViewCellValueEventArgs e)
    {
        e.Value = store[e.RowIndex];
    }

    private void dataGridView1_CellValuePushed(object sender, 
        DataGridViewCellValueEventArgs e)
    {
        store[e.RowIndex] = (LightStatus) e.Value;
    }
#endregion

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new TriValueVirtualCheckBox());
    }
}

public enum LightStatus
{
    Unknown, 
    TurnedOn, 
    TurnedOff
};


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2016 Microsoft