Export (0) Print
Expand All

DataGridViewCheckBoxCell Class

Displays a check box user interface (UI) to use in a DataGridView control.

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

public class DataGridViewCheckBoxCell : DataGridViewCell, IDataGridViewEditingCell
public class DataGridViewCheckBoxCell extends DataGridViewCell implements IDataGridViewEditingCell
public class DataGridViewCheckBoxCell extends DataGridViewCell implements IDataGridViewEditingCell
Not applicable.

The DataGridViewCheckBoxCell class is a specialized type of DataGridViewCell used to display binary or ternary information presented through a check box UI. In the default binary mode, the cells can hold values of true or false. In ternary mode, the cells take on one of the CheckState values. The ThreeState property determines whether binary or ternary mode is in effect.

The DataGridViewCheckBoxColumn is the column type specialized to hold cells of this type. To model the cells within a column after an existing DataGridViewCheckBoxCell, set the column's CellTemplate property to that cell. By default, the CellTemplate is initialized to a new DataGridViewCheckBoxCell.

The cell-related properties of the column are wrappers for the similarly-named properties of the template cell. Changing the property values of the template cell will affect only cells based on the template that are added after the change. Changing the cell-related property values of the column, however, will update the template cell and all other cells in the column, and refresh the column display if necessary.

When the current cell is a DataGridViewCheckBoxCell, the DataGridView.IsCurrentCellInEditMode property is always true.

Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

Notes to Inheritors: When you derive from DataGridViewCheckBoxCell and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell.

The following code example uses a DataGridViewCheckBoxColumn to track which employees are in the office. The column is made up of DataGridViewCheckBoxCell objects. This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

private void AddOutOfOfficeColumn()
{
    DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
    {
        column.HeaderText = ColumnName.OutOfOffice.ToString();
        column.Name = ColumnName.OutOfOffice.ToString();
        column.AutoSizeMode = 
            DataGridViewAutoSizeColumnMode.DisplayedCells;
        column.FlatStyle = FlatStyle.Standard;
        column.ThreeState = true;
        column.CellTemplate = new DataGridViewCheckBoxCell();
        column.CellTemplate.Style.BackColor = Color.Beige;
    }

    DataGridView1.Columns.Insert(0, column);
}

System.Object
   System.Windows.Forms.DataGridViewElement
     System.Windows.Forms.DataGridViewCell
      System.Windows.Forms.DataGridViewCheckBoxCell

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft