与 DataRow 的数据绑定 - 问题

发布于 2024-08-08 15:13:30 字数 602 浏览 6 评论 0原文

我目前正在使用 c# 编写一个项目,但遇到了问题。我将简要描述我的表单:

它在表单的下半部分有一个 datagridview,可以选择单行,并且是只读的。在表单的上半部分,我有各种组件,它们数据绑定到 datagridview 上的选定行。我这样做是因为数据库中的列太多,无法在 datagridview 中显示,而不会严重扰乱 UI。这工作正常,并且格式和解析事件被正确调用。

我有一个“添加新记录”按钮,它将上半部分的控件重新绑定到新数据行(尚不是数据表的一部分),这就是我遇到问题的地方。首先,当直接绑定到数据行时,控件似乎不像绑定到数据表时那样对 DBNull 值满意。但这很好,我只是在绑定新行之前为其分配了默认值。但是,由于某种原因,当直接绑定到行时,格式和解析事件似乎不会被触发,我不明白为什么?我需要这些的主要原因是我有一个日期值列,理想情况下需要为空(从 UI 角度来看为空),因此我将 DateTime.MinValue 指定为新行上的默认值,然后我在格式上查找此值/ 解析事件并进行必要的转换。

我想我的主要问题是,当绑定到同一列中具有相同空值的数据表时,为什么绑定到数据行会引发 DBNull 异常,并且列约束具有“允许空值”。我的另一个问题是,当组件绑定到数据行时,为什么不触发 Format / Parse 事件?

I am currently coding a project under c# and am experiencing problems. I'll give a brief description of my form:

It has a datagridview on the bottom half of the form with single row selection, and is read only. On the top half of the form I have various components which are databound to the selected row on the datagridview. I did this because there was too many columns in the database to show in the datagridview without seriously cluttering up the UI. This is working fine and the format and parse events are being called correctly.

I have a Add New Record button which rebinds the controls on the top half to a new datarow (which isn't part of the datatable yet) and this is where I'm having problems. Firstly when binding directly to a datarow it seems the controls arn't as happy with DBNull values as they are when binding to a datatable. But that's fine I just assigned default values to the new row before binding it. However, it seems the Format and Parse events are not fired when bound directly to a row for some reason, and I can't work out why? The main reason I need these is I have a Date value column which ideally needs to be null (blank from a UI perspective) so I assign the DateTime.MinValue as a default value on the new row and then I look for this on the format / parse event and do the nessessary convertions.

I guess my main question is why does binding to a datarow raise DBNull exceptions when binding to a datatable with the same null values in the same columns raises no exception and the column constraints has 'Allow Nulls'. My alternative question is why aren't the Format / Parse events fired when a component is bound to a datarow?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文