DataGridView
(命名)
dataGridView
DataTable
(命名)
子数据表
dataGridView
有一个
DataGridViewCheckBoxColumn
(命名)
parentId列
父节点
儿童数据行
在里面
.
父节点
数据表
在
DataSet
父数据表
).在这种情况下,孩子可能只有一位家长。
(及有关
ParentDataTable.ID
Guid
ChildDataTable.ParentID
允许空值。空值表示该子级与任何父级“断开连接”,我希望它显示在中
dataGridView
作为一个未经检查的
CheckBox
parentId列
(为了澄清,标记为“有父项”)。
TrueValue
和
FalseValue
上的属性
通过创建实现相等和比较操作的自定义类型:
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;
}
}
…然后将它们设置为上的真/假值
parentId列
parentIDColumn.TrueValue = new NotDBNull();
parentIDColumn.FalseValue = new IsDBNull();
但调试器从未命中我的断点,这表明我错过了机会。在显示时,我得到以下错误
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
---------------------------
所以我知道我的定制没有实现。format调用似乎发生在单元格级别(而不是列),因此我不确定是否有更好/不同的方式我遗漏了。