如何让 DataGridViewCheckBoxColumn 显示 DataRelation 状态?

发布于 2024-07-17 17:11:02 字数 3169 浏览 3 评论 0原文

假设我有一个 DataGridView(名为 dataGridView),它显示强类型 DataTable(名为 ChildDataTable)。

dataGridView 有一个DataGridViewCheckBoxColumn(名为parentIDColumn),它绑定到每个ChildDataRow 上的字段属性ParentID ChildDataTable中。

ParentID 是一个外键,它将 ChildDataTableDataSet 中的另一个 DataTable(创造性地命名为 ParentDataTable< /em>)。 在这种情况下,一个孩子可能只有一个父母。 ParentID(以及相关的ParentDataTable.ID)字段的类型为Guid

ChildDataTable.ParentID 允许空值。 null 值表示子级与任何父级“断开连接”,我希望它在 dataGridView 中显示为 parentIDColumn 列中未选中的 CheckBox >(出于澄清目的,标记为“有父级”)。

我尝试通过创建实现相等和比较操作的自定义类型来操作 parentIDColumn 上的 TrueValueFalseValue 属性:

    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;
        }
    }

...然后设置它们作为 parentIDColumn 上的 True/False 值:

        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   
---------------------------

所以我知道我的自定义尚未实现。 格式调用似乎发生在单元格级别(而不是列),所以我不确定是否有更好/不同的方式我错过了。

如何为复选框列注入 True/False 逻辑,以便选中表示外键不为空,未选中表示外键为空?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-07-24 17:11:02

花了一点时间,但我现在有了一个可行的解决方案。

我向 ChildDataTable 添加了一个新的计算列(名为 HasParent),该列使用表达式“ParentID IS NOT NULL”并且属于布尔类型。 一旦绑定到parentIDColumn,这一切就完美了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文