DataGridBoolColumn Class
Assembly: System.Windows.Forms (in system.windows.forms.dll)
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.
Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.ComponentModel Public Class MyForm Inherits System.Windows.Forms.Form Private components As System.ComponentModel.Container Private myTable As DataTable Private myGrid As DataGrid = New DataGrid() Public Shared Sub Main() Application.Run(New MyForm()) End Sub Public Sub New() Try InitializeComponent() myTable = New DataTable("NamesTable") myTable.Columns.Add(New DataColumn("Name")) Dim column As DataColumn = New DataColumn _ ("id", GetType(System.Int32)) myTable.Columns.Add(column) myTable.Columns.Add(New DataColumn _ ("calculatedField", GetType(Boolean))) Dim namesDataSet As DataSet = New DataSet("myDataSet") namesDataSet.Tables.Add(myTable) myGrid.SetDataBinding(namesDataSet, "NamesTable") AddData() AddTableStyle() Catch exc As System.Exception Console.WriteLine(exc.ToString) End Try End Sub Private Sub AddTableStyle() ' Map a new TableStyle to the DataTable. Then ' add DataGridColumnStyle objects to the collection ' of column styles with appropriate mappings. Dim dgt As DataGridTableStyle = New DataGridTableStyle() dgt.MappingName = "NamesTable" Dim dgtbc As DataGridTextBoxColumn = _ 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) Dim db As DataGridBoolColumnInherit = _ 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" End Sub Private Sub 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. Dim dRow As 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() End Sub Private Sub InitializeComponent() Me.Size = New Size(500, 500) myGrid.Size = New Size(350, 250) myGrid.TabStop = True myGrid.TabIndex = 1 Me.StartPosition = FormStartPosition.CenterScreen Me.Controls.Add(myGrid) End Sub End Class Public Class DataGridBoolColumnInherit Inherits DataGridBoolColumn Private trueBrush As SolidBrush = Brushes.Blue Private falseBrush As SolidBrush = Brushes.Yellow Private expressionColumn As DataColumn = Nothing Shared count As Int32 = 0 Public Property FalseColor() As Color Get Return falseBrush.Color End Get Set(ByVal Value As Color) falseBrush = New SolidBrush(Value) Invalidate() End Set End Property Public Property TrueColor() As Color Get Return trueBrush.Color End Get Set(ByVal Value As Color) trueBrush = New SolidBrush(Value) Invalidate() End Set End Property Public Sub New() count += 1 End Sub ' This will work only with a DataSet or DataTable. ' The code is not compatible with IBindingList implementations. Public Property Expression() As String Get If Me.expressionColumn Is Nothing Then Return String.Empty Else Return Me.expressionColumn.Expression End If End Get Set(ByVal Value As String) If expressionColumn Is Nothing Then AddExpressionColumn(Value) Else expressionColumn.Expression = Value End If If Not (expressionColumn Is Nothing) And expressionColumn.Expression.Equals(Value) Then Return End If Invalidate() End Set End Property Private Sub AddExpressionColumn(ByVal value As String) ' Get the grid's data source. First check for a null ' table or data grid. If Me.DataGridTableStyle Is Nothing Or _ Me.DataGridTableStyle.DataGrid Is Nothing Then Return End If Dim dg As DataGrid = Me.DataGridTableStyle.DataGrid Dim dv As DataView = CType(dg.BindingContext(dg.DataSource, dg.DataMember), CurrencyManager).List ' This works only with System.Data.DataTable. If dv Is Nothing Then Return End If ' 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. Dim col As DataColumn = dv.Table.Columns("__Computed__Column__") If Not (col Is Nothing) Then Return End If col = New DataColumn("__Computed__Column__" + count.ToString()) dv.Table.Columns.Add(col) col.Expression = value expressionColumn = col End Sub ' Override the OnPaint method to paint the cell based on the expression. Protected Overloads Overrides Sub Paint _ (ByVal g As Graphics, _ ByVal bounds As Rectangle, _ ByVal [source] As CurrencyManager, _ ByVal rowNum As Integer, _ ByVal backBrush As Brush, _ ByVal foreBrush As Brush, _ ByVal alignToRight As Boolean) Dim trueExpression As Boolean = False Dim hasExpression As Boolean = False Dim drv As DataRowView = [source].List(rowNum) hasExpression = Not (Me.expressionColumn Is Nothing) And Not (Me.expressionColumn.Expression Is Nothing) And Not Me.expressionColumn.Expression.Equals([String].Empty) ' Get the value from the expression column. ' For simplicity, we assume a True/False value for the ' expression column. If hasExpression Then Dim expr As Object = drv.Row(expressionColumn.ColumnName) trueExpression = expr.Equals("True") End If ' Let the DataGridBoolColumn do the painting. If Not hasExpression Then MyBase.Paint(g, bounds, [source], rowNum, backBrush, foreBrush, alignToRight) End If ' Paint using the expression color for true or false, as calculated. If trueExpression Then MyBase.Paint(g, bounds, [source], rowNum, trueBrush, foreBrush, alignToRight) Else MyBase.Paint(g, bounds, [source], rowNum, falseBrush, foreBrush, alignToRight) End If End Sub End Class
import System.*;
import System.Data.*;
import System.Windows.Forms.*;
import System.Drawing.*;
import System.ComponentModel.*;
public class MyForm extends Form
{
private DataTable myTable;
private DataGrid myGrid = new DataGrid();
public MyForm()
{
try {
InitializeComponent();
myTable = new DataTable("NamesTable");
myTable.get_Columns().Add(new DataColumn("Name"));
DataColumn column =
new DataColumn("id", System.Int32.class.ToType());
myTable.get_Columns().Add(column);
myTable.get_Columns().Add(new DataColumn("calculatedField",
boolean.class.ToType()));
DataSet namesDataSet = new DataSet();
namesDataSet.get_Tables().Add(myTable);
myGrid.SetDataBinding(namesDataSet, "NamesTable");
AddTableStyle();
AddData();
}
catch(System.Exception exc) {
Console.WriteLine(exc.ToString());
}
} //MyForm
private void gridEnter(Object sender, EventArgs e)
{
myGrid.set_CurrentCell(new DataGridCell(2, 2));
} //gridEnter
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.set_MappingName("NamesTable");
DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn();
dgtbc.set_MappingName("Name");
dgtbc.set_HeaderText("Name");
dgt.get_GridColumnStyles().Add(dgtbc);
dgtbc = new DataGridTextBoxColumn();
dgtbc.set_MappingName("id");
dgtbc.set_HeaderText("id");
dgt.get_GridColumnStyles().Add(dgtbc);
DataGridBoolColumnInherit db = new DataGridBoolColumnInherit();
db.set_HeaderText("less than 1000 = blue");
db.set_Width(150);
db.set_MappingName("calculatedField");
dgt.get_GridColumnStyles().Add(db);
myGrid.get_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.set_Expression("id < 1000");
} //AddTableStyle
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.set_Item("Name", "name 1 ");
dRow.set_Item("id", (Int32)999);
myTable.get_Rows().Add(dRow);
dRow = myTable.NewRow();
dRow.set_Item("Name", "name 2");
dRow.set_Item("id", (Int32)2300);
myTable.get_Rows().Add(dRow);
dRow = myTable.NewRow();
dRow.set_Item("Name", "name 3");
dRow.set_Item("id", (Int32)120);
myTable.get_Rows().Add(dRow);
dRow = myTable.NewRow();
dRow.set_Item("Name", "name 4");
dRow.set_Item("id", (Int32)4023);
myTable.get_Rows().Add(dRow);
dRow = myTable.NewRow();
dRow.set_Item("Name", "name 5");
dRow.set_Item("id", (Int32)2345);
myTable.get_Rows().Add(dRow);
myTable.AcceptChanges();
} //AddData
private void InitializeComponent()
{
this.set_Size(new Size(500, 500));
myGrid.set_Size(new Size(350, 250));
myGrid.set_TabStop(true);
myGrid.set_TabIndex(1);
this.set_StartPosition(FormStartPosition.CenterScreen);
this.get_Controls().Add(myGrid);
} //InitializeComponent
/** @attribute STAThread()
*/
public static void main(String[] args)
{
MyForm myGridForm = new MyForm();
myGridForm.ShowDialog();
} //main
} //MyForm
public class DataGridBoolColumnInherit extends DataGridBoolColumn
{
private SolidBrush trueBrush = (SolidBrush)Brushes.get_Blue();
private SolidBrush falseBrush = (SolidBrush)Brushes.get_Yellow();
private DataColumn expressionColumn = null;
private static int count = 0;
/** @property
*/
public Color get_FalseColor()
{
return falseBrush.get_Color();
} //get_FalseColor
/** @property
*/
public void set_FalseColor(Color value)
{
falseBrush = new SolidBrush(value);
Invalidate();
} //set_FalseColor
/** @property
*/
public Color get_TrueColor()
{
return trueBrush.get_Color();
} //get_TrueColor
/** @property
*/
public void set_TrueColor(Color value)
{
trueBrush = new SolidBrush(value);
Invalidate();
} //set_TrueColor
public DataGridBoolColumnInherit()
{
super();
count++;
} //DataGridBoolColumnInherit
// This will work only with a DataSet or DataTable.
// The code is not compatible with IBindingList implementations.
/** @property
*/
public String get_Expression()
{
return (this.expressionColumn == null)
? String.Empty : this.expressionColumn.get_Expression();
} //get_Expression
/** @property
*/
public void set_Expression(String value)
{
if(expressionColumn == null) {
AddExpressionColumn(value);
}
else {
expressionColumn.set_Expression(value);
}
if(expressionColumn != null
&& expressionColumn.get_Expression().Equals(value)) {
return;
}
Invalidate();
} //set_Expression
private void AddExpressionColumn(String value)
{
// Get the grid's data source. First check for a null
// table or data grid.
if(this.get_DataGridTableStyle() == null
|| this.get_DataGridTableStyle().get_DataGrid() == null) {
return;
}
DataGrid myGrid = this.get_DataGridTableStyle().get_DataGrid();
DataView myDataView = (DataView)(((CurrencyManager)(myGrid.
get_BindingContext().get_Item(myGrid.get_DataSource(),
myGrid.get_DataMember()))).get_List());
// 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.get_Table().get_Columns().
get_Item("__Computed__Column__");
if(col != null) {
return;
}
col = new DataColumn("__Computed__Column__"
+ System.Convert.ToString(count));
myDataView.get_Table().get_Columns().Add(col);
col.set_Expression(value);
expressionColumn = col;
} //AddExpressionColumn
// override the OnPaint method to paint the cell based on the expression.
protected void Paint(Graphics g, Rectangle bounds, CurrencyManager source,
int rowNum, Brush backBrush, Brush foreBrush, boolean alignToRight)
{
boolean trueExpression = false;
boolean hasExpression = false;
DataRowView drv = (DataRowView)source.get_List().get_Item(rowNum);
hasExpression = this.expressionColumn != null
&& this.expressionColumn.get_Expression() != null
&& !(this.expressionColumn.get_Expression().Equals(String.Empty));
Console.WriteLine(String.Format("hasExpressionValue {0}",
System.Convert.ToString(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.get_Row().get_Item(expressionColumn.get_ColumnName());
trueExpression = expr.Equals("True");
}
// Let the DataGridBoolColumn do the painting.
if(!(hasExpression)) {
super.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
// Paint using the expression color for true or false, as calculated.
if(trueExpression) {
super.Paint(g, bounds, source, rowNum, trueBrush, foreBrush,
alignToRight);
}
else {
super.Paint(g, bounds, source, rowNum, falseBrush, foreBrush,
alignToRight);
}
} //Paint
} //DataGridBoolColumnInherit
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.DataGridColumnStyle
System.Windows.Forms.DataGridBoolColumn
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.