Export (0) Print
Expand All

DataGridBoolColumn Class

Specifies a column in which each cell contains a check box for representing a Boolean value.

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

public class DataGridBoolColumn : DataGridColumnStyle

The DataGridBoolColumn type exposes the following members.

Public methodDataGridBoolColumn()Initializes a new instance of the DataGridBoolColumn class.
Public methodDataGridBoolColumn(PropertyDescriptor)Initializes a new instance of a DataGridBoolColumn with the specified PropertyDescriptor.
Public methodDataGridBoolColumn(PropertyDescriptor, Boolean)Initializes a new instance of a DataGridBoolColumn with the specified PropertyDescriptor, and specifying whether the column style is a default column.

Public propertyAlignmentGets or sets the alignment of text in a column. (Inherited from DataGridColumnStyle.)
Public propertyAllowNullGets or sets a value indicating whether null values are allowed.
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event. (Inherited from Component.)
Public propertyContainerGets the IContainer that contains the Component. (Inherited from Component.)
Public propertyDataGridTableStyleGets the DataGridTableStyle for the column. (Inherited from DataGridColumnStyle.)
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected propertyEventsGets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public propertyFalseValueGets or sets the actual value used when setting the value of the column to false.
Protected propertyFontHeightGets the height of the column's font. (Inherited from DataGridColumnStyle.)
Public propertyHeaderAccessibleObjectInfrastructure. Gets the AccessibleObject for the column. (Inherited from DataGridColumnStyle.)
Public propertyHeaderTextGets or sets the text of the column header. (Inherited from DataGridColumnStyle.)
Public propertyMappingNameGets or sets the name of the data member to map the column style to. (Inherited from DataGridColumnStyle.)
Public propertyNullTextGets or sets the text that is displayed when the column contains null. (Inherited from DataGridColumnStyle.)
Public propertyNullValueGets or sets the actual value used when setting the value of the column to Value.
Public propertyPropertyDescriptorGets or sets the PropertyDescriptor that determines the attributes of data displayed by the DataGridColumnStyle. (Inherited from DataGridColumnStyle.)
Public propertyReadOnlyGets or sets a value indicating whether the data in the column can be edited. (Inherited from DataGridColumnStyle.)
Public propertySiteGets or sets the ISite of the Component. (Inherited from Component.)
Public propertyTrueValueGets or sets the actual value used when setting the value of the column to true.
Public propertyWidthGets or sets the width of the column. (Inherited from DataGridColumnStyle.)

Protected methodAbortInitiates a request to interrupt an edit procedure. (Overrides DataGridColumnStyle.Abort(Int32).)
Protected methodBeginUpdateSuspends the painting of the column until the EndUpdate method is called. (Inherited from DataGridColumnStyle.)
Protected methodCheckValidDataSourceThrows an exception if the System.Windows.Forms.DataGrid does not have a valid data source, or if this column is not mapped to a valid property in the data source. (Inherited from DataGridColumnStyle.)
Protected methodColumnStartedEditingInforms the System.Windows.Forms.DataGrid that the user has begun editing the column. (Inherited from DataGridColumnStyle.)
Protected methodCommitInitiates a request to complete an editing procedure. (Overrides DataGridColumnStyle.Commit(CurrencyManager, Int32).)
Protected methodConcedeFocusNotifies a column that it must relinquish the focus to the control it is hosting. (Overrides DataGridColumnStyle.ConcedeFocus().)
Protected methodCreateHeaderAccessibleObjectGets the AccessibleObject for the column. (Inherited from DataGridColumnStyle.)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the Component. (Inherited from Component.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the Component and optionally releases the managed resources. (Inherited from Component.)
Protected methodEdit(CurrencyManager, Int32, Rectangle, Boolean)Prepares a cell for editing. (Inherited from DataGridColumnStyle.)
Protected methodEdit(CurrencyManager, Int32, Rectangle, Boolean, String)Prepares the cell for editing using the specified CurrencyManager, row number, and Rectangle parameters. (Inherited from DataGridColumnStyle.)
Protected methodEdit(CurrencyManager, Int32, Rectangle, Boolean, String, Boolean)Prepares the cell for editing a value. (Overrides DataGridColumnStyle.Edit(CurrencyManager, Int32, Rectangle, Boolean, String, Boolean).)
Protected methodEndUpdateResumes the painting of columns suspended by calling the BeginUpdate method. (Inherited from DataGridColumnStyle.)
Protected methodEnterNullValueEnters a Value into the column. (Overrides DataGridColumnStyle.EnterNullValue().)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Protected methodGetColumnValueAtRowGets the value at the specified row. (Overrides DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager, Int32).)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetMinimumHeightGets the height of a cell in a column. (Overrides DataGridColumnStyle.GetMinimumHeight().)
Protected methodGetPreferredHeightGets the height used when resizing columns. (Overrides DataGridColumnStyle.GetPreferredHeight(Graphics, Object).)
Protected methodGetPreferredSizeGets the optimum width and height of a cell given a specific value to contain. (Overrides DataGridColumnStyle.GetPreferredSize(Graphics, Object).)
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodInvalidateRedraws the column and causes a paint message to be sent to the control. (Inherited from DataGridColumnStyle.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected methodPaint(Graphics, Rectangle, CurrencyManager, Int32)Infrastructure. Draws the DataGridBoolColumn with the given Graphics, Rectangle and row number. (Overrides DataGridColumnStyle.Paint(Graphics, Rectangle, CurrencyManager, Int32).)
Protected methodPaint(Graphics, Rectangle, CurrencyManager, Int32, Boolean)Infrastructure. Draws the DataGridBoolColumn with the given Graphics, Rectangle, row number, and alignment settings. (Overrides DataGridColumnStyle.Paint(Graphics, Rectangle, CurrencyManager, Int32, Boolean).)
Protected methodPaint(Graphics, Rectangle, CurrencyManager, Int32, Brush, Brush, Boolean)Draws the DataGridBoolColumn with the given Graphics, Rectangle, row number, Brush, and Color. (Overrides DataGridColumnStyle.Paint(Graphics, Rectangle, CurrencyManager, Int32, Brush, Brush, Boolean).)
Protected methodReleaseHostedControlAllows the column to free resources when the control it hosts is not needed. (Inherited from DataGridColumnStyle.)
Public methodResetHeaderTextResets the HeaderText to its default value, null. (Inherited from DataGridColumnStyle.)
Protected methodSetColumnValueAtRowSets the value of a specified row. (Overrides DataGridColumnStyle.SetColumnValueAtRow(CurrencyManager, Int32, Object).)
Protected methodSetDataGridSets the System.Windows.Forms.DataGrid control that this column belongs to. (Inherited from DataGridColumnStyle.)
Protected methodSetDataGridInColumnSets the System.Windows.Forms.DataGrid for the column. (Inherited from DataGridColumnStyle.)
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Protected methodUpdateUIUpdates the value of a specified row with the given text. (Inherited from DataGridColumnStyle.)

Public eventAlignmentChangedOccurs when the Alignment property value changes. (Inherited from DataGridColumnStyle.)
Public eventAllowNullChangedOccurs when the AllowNull property is changed.
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public eventFalseValueChangedOccurs when the FalseValue property is changed.
Public eventFontChangedOccurs when the column's font changes. (Inherited from DataGridColumnStyle.)
Public eventHeaderTextChangedOccurs when the HeaderText property value changes. (Inherited from DataGridColumnStyle.)
Public eventMappingNameChangedOccurs when the MappingName value changes. (Inherited from DataGridColumnStyle.)
Public eventNullTextChangedOccurs when the NullText value changes. (Inherited from DataGridColumnStyle.)
Public eventPropertyDescriptorChangedOccurs when the PropertyDescriptor property value changes. (Inherited from DataGridColumnStyle.)
Public eventReadOnlyChangedOccurs when the ReadOnly property value changes. (Inherited from DataGridColumnStyle.)
Public eventTrueValueChangedOccurs when the TrueValue property value is changed.
Public eventWidthChangedOccurs when the Width property value changes. (Inherited from DataGridColumnStyle.)

Explicit interface implemetationPrivate methodIDataGridColumnStyleEditingNotificationService.ColumnStartedEditingInforms the DataGrid control that the user has begun editing the column. (Inherited from DataGridColumnStyle.)

The DataGridBoolColumn derives from the abstract class DataGridColumnStyle. At run time, the DataGridBoolColumn contains check boxes in each cell that have three states by default: checked (true), unchecked (false), and Value. To use two-state check boxes, set the AllowNull property to false.

Properties added to the class include FalseValue, NullValue, and TrueValue. These properties specify the value underlying each of the column's states.

The following code example first creates a new DataGridBoolColumn and adds it to the GridColumnStylesCollection of a DataGridTableStyle.

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

public class MyForm : Form 
    private DataTable myTable;
    private DataGrid myGrid = new DataGrid();

    public MyForm() : base() 

            myTable = new DataTable("NamesTable");
            myTable.Columns.Add(new DataColumn("Name"));
            DataColumn column = new DataColumn
                ("id", typeof(System.Int32));
                DataColumn("calculatedField", typeof(bool)));
            DataSet namesDataSet = new DataSet();
            myGrid.SetDataBinding(namesDataSet, "NamesTable");

        catch (System.Exception exc)

    private void grid_Enter(object sender, EventArgs e) 
        myGrid.CurrentCell = new DataGridCell(2,2);

    private void AddTableStyle()
        // Map a new  TableStyle to the DataTable. Then  
        // add DataGridColumnStyle objects to the collection 
        // of column styles with appropriate mappings.
        DataGridTableStyle dgt = new DataGridTableStyle();
        dgt.MappingName = "NamesTable";

        DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn();
        dgtbc.MappingName = "Name";
        dgtbc.HeaderText= "Name";

        dgtbc = new DataGridTextBoxColumn();
        dgtbc.MappingName = "id";
        dgtbc.HeaderText= "id";

        DataGridBoolColumnInherit db = 
            new DataGridBoolColumnInherit();
        db.HeaderText= "less than 1000 = blue";
        db.Width= 150;
        db.MappingName = "calculatedField";


        // This expression instructs the grid to change 
        // the color of the inherited DataGridBoolColumn 
        // according to the value of the id field. If it's 
        // less than 1000, the row is blue. Otherwise, 
        // the color is yellow.
        db.Expression = "id < 1000";

    private void AddData() 
        // Add data with varying numbers for the id field. 
        // If the number is over 1000, the cell will paint 
        // yellow. Otherwise, it will be blue.
        DataRow dRow = myTable.NewRow();

        dRow["Name"] = "name 1 ";
        dRow["id"] = 999;

        dRow = myTable.NewRow();
        dRow["Name"] = "name 2";
        dRow["id"] = 2300;

        dRow = myTable.NewRow();
        dRow["Name"] = "name 3";
        dRow["id"] = 120;

        dRow = myTable.NewRow();
        dRow["Name"] = "name 4";
        dRow["id"] = 4023;

        dRow = myTable.NewRow();
        dRow["Name"] = "name 5";
        dRow["id"] = 2345;


    private void InitializeComponent() 
        this.Size = new Size(500, 500);
        myGrid.Size = new Size(350, 250);
        myGrid.TabStop = true;
        myGrid.TabIndex = 1;

        this.StartPosition = FormStartPosition.CenterScreen;
    public static void Main() 
        Application.Run(new MyForm());

public class DataGridBoolColumnInherit : DataGridBoolColumn 
    private SolidBrush trueBrush = Brushes.Blue as SolidBrush;
    private SolidBrush falseBrush = Brushes.Yellow as SolidBrush;
    private DataColumn expressionColumn = null;
    private static int count = 0;

    public Color FalseColor 
            return falseBrush.Color;
            falseBrush = new SolidBrush(value);

    public Color TrueColor 
            return trueBrush.Color;
            trueBrush = new SolidBrush(value);

    public DataGridBoolColumnInherit() : base () 
        count ++;

    // This will work only with a DataSet or DataTable. 
    // The code is not compatible with IBindingList implementations. 
    public string Expression 
            return this.expressionColumn == null ? String.Empty : 
            if (expressionColumn == null)
                expressionColumn.Expression = value;
            if (expressionColumn != null && 

    private void AddExpressionColumn(string value) 
        // Get the grid's data source. First check for a null  
        // table or data grid. 
        if (this.DataGridTableStyle == null || 
            this.DataGridTableStyle.DataGrid == null)

        DataGrid myGrid = this.DataGridTableStyle.DataGrid;
        DataView myDataView = ((CurrencyManager) 
            as DataView;

        // This works only with System.Data.DataTable. 
        if (myDataView == null)

        // If the user already added a column with the name  
        // then exit. Otherwise, add the column and set the  
        // expression to the value passed to this function.
        DataColumn col = myDataView.Table.Columns["__Computed__Column__"];
        if (col != null)
        col = new DataColumn("__Computed__Column__" + count.ToString());

        col.Expression = value;
        expressionColumn = col;

    // override the OnPaint method to paint the cell based on the expression. 
    protected override void Paint(Graphics g, Rectangle bounds,
        CurrencyManager source, int rowNum,
        Brush backBrush, Brush foreBrush,
        bool alignToRight) 
        bool trueExpression = false;
        bool hasExpression = false;
        DataRowView drv = source.List[rowNum] as DataRowView;

        hasExpression = this.expressionColumn != null && 
            this.expressionColumn.Expression != null && 

        Console.WriteLine(string.Format("hasExpressionValue {0}",hasExpression));
        // Get the value from the expression column. 
        // For simplicity, we assume a True/False value for the  
        // expression column. 
        if (hasExpression) 
            object expr = drv.Row[expressionColumn.ColumnName];
            trueExpression = expr.Equals("True");

        // Let the DataGridBoolColumn do the painting. 
        if (!hasExpression)
            base.Paint(g, bounds, source, rowNum, 
                backBrush, foreBrush, alignToRight);

        // Paint using the expression color for true or false, as calculated. 
        if (trueExpression)
            base.Paint(g, bounds, source, rowNum, 
                trueBrush, foreBrush, alignToRight);
            base.Paint(g, bounds, source, rowNum, 
                falseBrush, foreBrush, alignToRight);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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