DataGridBoolColumn Class
TOC
Collapse the table of content
Expand the table of content

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)

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Windows.Forms.DataGridColumnStyle
        System.Windows.Forms.DataGridBoolColumn

public class DataGridBoolColumn : DataGridColumnStyle

NameDescription
System_CAPS_pubmethodDataGridBoolColumn()

Initializes a new instance of the DataGridBoolColumn class.

System_CAPS_pubmethodDataGridBoolColumn(PropertyDescriptor)

Initializes a new instance of a DataGridBoolColumn with the specified PropertyDescriptor.

System_CAPS_pubmethodDataGridBoolColumn(PropertyDescriptor, Boolean)

Initializes a new instance of a DataGridBoolColumn with the specified PropertyDescriptor, and specifying whether the column style is a default column.

NameDescription
System_CAPS_pubpropertyAlignment

Gets or sets the alignment of text in a column.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyAllowNull

Gets or sets a value indicating whether null values are allowed.

System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_pubpropertyDataGridTableStyle

Gets the DataGridTableStyle for the column.(Inherited from DataGridColumnStyle.)

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyFalseValue

Gets or sets the actual value used when setting the value of the column to false.

System_CAPS_protpropertyFontHeight

Gets the height of the column's font.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyHeaderAccessibleObject

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the AccessibleObject for the column.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyHeaderText

Gets or sets the text of the column header.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyMappingName

Gets or sets the name of the data member to map the column style to.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyNullText

Gets or sets the text that is displayed when the column contains null.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyNullValue

Gets or sets the actual value used when setting the value of the column to Value.

System_CAPS_pubpropertyPropertyDescriptor

Gets or sets the PropertyDescriptor that determines the attributes of data displayed by the DataGridColumnStyle.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertyReadOnly

Gets or sets a value indicating whether the data in the column can be edited.(Inherited from DataGridColumnStyle.)

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

System_CAPS_pubpropertyTrueValue

Gets or sets the actual value used when setting the value of the column to true.

System_CAPS_pubpropertyWidth

Gets or sets the width of the column.(Inherited from DataGridColumnStyle.)

NameDescription
System_CAPS_protmethodAbort(Int32)

Initiates a request to interrupt an edit procedure.(Overrides DataGridColumnStyle.Abort(Int32).)

System_CAPS_protmethodBeginUpdate()

Suspends the painting of the column until the EndUpdate method is called.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodCheckValidDataSource(CurrencyManager)

Throws 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.)

System_CAPS_protmethodColumnStartedEditing(Control)

Informs the System.Windows.Forms.DataGrid that the user has begun editing the column.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodCommit(CurrencyManager, Int32)

Initiates a request to complete an editing procedure.(Overrides DataGridColumnStyle.Commit(CurrencyManager, Int32).)

System_CAPS_protmethodConcedeFocus()

Notifies a column that it must relinquish the focus to the control it is hosting.(Overrides DataGridColumnStyle.ConcedeFocus().)

System_CAPS_protmethodCreateHeaderAccessibleObject()

Gets the AccessibleObject for the column.(Inherited from DataGridColumnStyle.)

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.(Inherited from Component.)

System_CAPS_protmethodEdit(CurrencyManager, Int32, Rectangle, Boolean)

Prepares a cell for editing.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodEdit(CurrencyManager, Int32, Rectangle, Boolean, String)

Prepares the cell for editing using the specified CurrencyManager, row number, and Rectangle parameters.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodEdit(CurrencyManager, Int32, Rectangle, Boolean, String, Boolean)
System_CAPS_protmethodEndUpdate()

Resumes the painting of columns suspended by calling the BeginUpdate method.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodEnterNullValue()

Enters a Value into the column.(Overrides DataGridColumnStyle.EnterNullValue().)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_protmethodGetColumnValueAtRow(CurrencyManager, Int32)

Gets the value at the specified row.(Overrides DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager, Int32).)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetMinimumHeight()

Gets the height of a cell in a column.(Overrides DataGridColumnStyle.GetMinimumHeight().)

System_CAPS_protmethodGetPreferredHeight(Graphics, Object)

Gets the height used when resizing columns.(Overrides DataGridColumnStyle.GetPreferredHeight(Graphics, Object).)

System_CAPS_protmethodGetPreferredSize(Graphics, Object)

Gets the optimum width and height of a cell given a specific value to contain.(Overrides DataGridColumnStyle.GetPreferredSize(Graphics, Object).)

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodInvalidate()

Redraws the column and causes a paint message to be sent to the control.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodPaint(Graphics, Rectangle, CurrencyManager, Int32)

This API supports the product infrastructure and is not intended to be used directly from your code. Draws the DataGridBoolColumn with the given Graphics, Rectangle and row number.(Overrides DataGridColumnStyle.Paint(Graphics, Rectangle, CurrencyManager, Int32).)

System_CAPS_protmethodPaint(Graphics, Rectangle, CurrencyManager, Int32, Boolean)

This API supports the product infrastructure and is not intended to be used directly from your code. Draws the DataGridBoolColumn with the given Graphics, Rectangle, row number, and alignment settings.(Overrides DataGridColumnStyle.Paint(Graphics, Rectangle, CurrencyManager, Int32, Boolean).)

System_CAPS_protmethodPaint(Graphics, Rectangle, CurrencyManager, Int32, Brush, Brush, Boolean)
System_CAPS_protmethodReleaseHostedControl()

Allows the column to free resources when the control it hosts is not needed.(Inherited from DataGridColumnStyle.)

System_CAPS_pubmethodResetHeaderText()

Resets the HeaderText to its default value, null.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodSetColumnValueAtRow(CurrencyManager, Int32, Object)
System_CAPS_protmethodSetDataGrid(DataGrid)

Sets the System.Windows.Forms.DataGrid control that this column belongs to.(Inherited from DataGridColumnStyle.)

System_CAPS_protmethodSetDataGridInColumn(DataGrid)

Sets the System.Windows.Forms.DataGrid for the column.(Inherited from DataGridColumnStyle.)

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

System_CAPS_protmethodUpdateUI(CurrencyManager, Int32, String)

Updates the value of a specified row with the given text.(Inherited from DataGridColumnStyle.)

NameDescription
System_CAPS_pubeventAlignmentChanged

Occurs when the Alignment property value changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventAllowNullChanged

Occurs when the AllowNull property is changed.

System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

System_CAPS_pubeventFalseValueChanged

Occurs when the FalseValue property is changed.

System_CAPS_pubeventFontChanged

Occurs when the column's font changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventHeaderTextChanged

Occurs when the HeaderText property value changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventMappingNameChanged

Occurs when the MappingName value changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventNullTextChanged

Occurs when the NullText value changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventPropertyDescriptorChanged

Occurs when the PropertyDescriptor property value changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventReadOnlyChanged

Occurs when the ReadOnly property value changes.(Inherited from DataGridColumnStyle.)

System_CAPS_pubeventTrueValueChanged

Occurs when the TrueValue property value is changed.

System_CAPS_pubeventWidthChanged

Occurs when the Width property value changes.(Inherited from DataGridColumnStyle.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIDataGridColumnStyleEditingNotificationService.ColumnStartedEditing(Control)

Informs 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() 
    {
        try
        {
            InitializeComponent();

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

            AddTableStyle();
            AddData();
        }
        catch (System.Exception exc)
        {
            Console.WriteLine(exc.ToString());
        }
    }


    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";
        dgt.GridColumnStyles.Add(dgtbc);

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

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

        myGrid.TableStyles.Add(dgt);

        // 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;
        myTable.Rows.Add(dRow);

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

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

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

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

        myTable.AcceptChanges();
    }

    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;
        this.Controls.Add(myGrid);
      }
    [STAThread]
    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 
    {
        get 
        {
            return falseBrush.Color;
        }
        set 
        {
            falseBrush = new SolidBrush(value);
            Invalidate();
        }
    }

    public Color TrueColor 
    {
        get 
        {
            return trueBrush.Color;
        }
        set 
        {
            trueBrush = new SolidBrush(value);
            Invalidate();
        }
    }

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


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

    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)
            return;

        DataGrid myGrid = this.DataGridTableStyle.DataGrid;
        DataView myDataView = ((CurrencyManager) 
            myGrid.BindingContext[myGrid.DataSource, 
            myGrid.DataMember]).List 
            as DataView;

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

        // 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)
            return;
        col = new DataColumn("__Computed__Column__" + count.ToString());

        myDataView.Table.Columns.Add(col);
        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 && 
            !this.expressionColumn.Expression.Equals(String.Empty);

        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);
        else
            base.Paint(g, bounds, source, rowNum, 
                falseBrush, foreBrush, alignToRight);
    }
}

.NET Framework
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft