views:

1394

answers:

1

Say I have a DataGridView (named dataGridView) that is displaying a Strongly Typed DataTable (named ChildDataTable).

dataGridView has a DataGridViewCheckBoxColumn (named parentIDColumn) that is bound to a Field Property ParentID on each ChildDataRow in ChildDataTable.

ParentID is a foreign key that relates ChildDataTable to another DataTable in the DataSet (creatively named ParentDataTable). In this case, a child may have only one parent. ParentID (and the related ParentDataTable.ID) field are of type Guid.

ChildDataTable.ParentID allows nulls. A null value represents that the child is "disconnected" from any parent, and I want this to display in dataGridView as an unchecked CheckBox in column parentIDColumn (labeled "Has Parent" for clarification purposes).

I have tried manipulating the TrueValue and FalseValue properties on parentIDColumn by creating custom types that implement equality and comparison operations:

    public class NotDBNull : IComparable
    {
        public override bool Equals(object obj)
        {
            return !obj.Equals(DBNull.Value);
        }

        public override int GetHashCode()
        {
            return Guid.Empty.GetHashCode();
        }

        public int CompareTo(object obj)
        {
            return Equals(obj) ? 0 : 1;
        }
    }

    public class IsDBNull : IComparable
    {
        public override bool Equals(object obj)
        {
            return obj.Equals(DBNull.Value);
        }

        public override int GetHashCode()
        {
            return DBNull.Value.GetHashCode();
        }

        public int CompareTo(object obj)
        {
            return Equals(obj) ? 0 : 1;
        }
    }

...and then setting them as the True/False values on parentIDColumn:

        parentIDColumn.TrueValue = new NotDBNull();
        parentIDColumn.FalseValue = new IsDBNull();

But the debugger never hits my breakpoint, suggesting that I have missed the boat. I get the following error upon display of dataGridView:

---------------------------
DataGridView Default Error Dialog
---------------------------
The following exception occurred in the DataGridView:

System.FormatException: Value '39df7d96-941a-4be9-a883-03182363bbab' cannot be converted to type 'Boolean'.
   at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)
   at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
   at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)

To replace this default dialog please handle the DataError event.
---------------------------
OK   
---------------------------

So I know that my customization is not getting reached. The format call seems to happen on a cell level (rather than column), so I wasn't sure if there was a better/different way that I was missing.

How do i inject my True/False logic for the checkbox column so that checked means the foreign key is not null, and unchecked means it is?

+1  A: 

Took a bit, but I now have a workable solution.

I added a new computed column (named HasParent) to ChildDataTable that used the expression "ParentID IS NOT NULL" and was of a Boolean type. Once bound to parentIDColumn, this worked perfectly.

ee