启用选项 strict 时,Linq 查询对 DataGridViewRow 存在隐式转换错误
我有一个 DataGridView 绑定到一个名为“BaseChange”的对象列表。 BaseChange 对象由 4 个属性组成...
- ChangeType
- ChangeStatus
- ChangeDescription
- LastChangeDate
datagridview 具有所有 4 个值以及第 5 个值的列(名为“colIsSelected”的复选框列)。 将列表绑定到网格并显示项目没有问题。
问题是,当启用选项 strict 时,获取网格中所选项目的查询会给出隐式转换错误。
这是查询...
Dim _changes As List(Of BaseChange)
_changes = (From _row As DataGridViewRow In dgvChanges.Rows() _
Where Convert.ToBoolean(_row.Cells(NAME_COLUMN_IS_SELECTED).Value) = True _
Select DirectCast(_row.DataBoundItem, BaseChange)).ToList()
...并且它会在选项严格关闭的情况下产生正确的结果。 隐式转换曲线位于“_row As DataGridViewRow”代码上,完整消息为“从“Object”到“System.Windows.Forms.DataGridViewRow”的隐式转换”。
如果我从查询中排除“As DataGridViewRow”,我会在 _row.Cells 和 _row.DataBoundItem 上收到延迟绑定错误,并且这也会导致选项严格失败。
我需要它在 VB 中启用 Option Strict 的情况下工作。 我在这里错过了什么吗? 有人有建议吗?
I have a DataGridView that is bound to a list of objects called "BaseChange". The BaseChange objects are made up of 4 properties...
- ChangeType
- ChangeStatus
- ChangeDescription
- LastChangeDate
The datagridview has columns for all 4 values as well as a 5th (a checkbox column called "colIsSelected"). There is no problem binding the list to the grid and displaying the items.
The problem is that the query that gets the selected items in the grid is giving me an implicit cast error when option strict is enabled.
This is the query...
Dim _changes As List(Of BaseChange)
_changes = (From _row As DataGridViewRow In dgvChanges.Rows() _
Where Convert.ToBoolean(_row.Cells(NAME_COLUMN_IS_SELECTED).Value) = True _
Select DirectCast(_row.DataBoundItem, BaseChange)).ToList()
...and it produces the correct results with option strict off. The implicit cast squiggle is on the "_row As DataGridViewRow" code, and the full message is "Implicit conversion from 'Object' to 'System.Windows.Forms.DataGridViewRow'".
If I exclude the "As DataGridViewRow" from the query, I get a late binding error on the _row.Cells and _row.DataBoundItem and this also fails option strict.
I need this to work with Option Strict enabled, and in VB. Am I missing something here? Anyone have suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 _row 对象的类型必须与集合类型的单一版本相匹配。
如:
Linq 将您的 Rows() 集合视为 IEnumerable,因此您的行是一个对象。 底部的解释更详细。
补充:
添加选项推断应该可以简化这一点。
有关更多详细信息,请参阅:
混合 VB.NET 的 Option Strict 和新的 Option Infer 指令的最佳方式是什么?
和
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/e3ec737a- 42f8-4767-a190-78390202a991/
说明:
我做了一些更多的挖掘来了解为什么它不简单。 DataGridView 的 RowCollection 实现了旧的 IEnumberable 接口,该接口返回对象,而较新的集合类型实现了 Generic IEnumerable(Of T) 接口,该接口直接返回类型,从而无需进行强制转换。
请参阅 msdn 了解实现的接口。
your type of the _row object has to match the single version of the collection type.
as in:
Linq sees your Rows() collection as IEnumerable, so your row is an object. Explanation at the bottom goes into more detail.
Added:
Adding Option Infer should simplify this.
See for more details:
What is the best way to mix VB.NET's Option Strict and the new Option Infer directives?
and
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/e3ec737a-42f8-4767-a190-78390202a991/
Explanation:
I did some more digging as to why it isn't simpler. RowCollection for a DataGridView implements the older IEnumberable interface which returns objects, while newer collection types Implement the Generic IEnumerable(Of T) Interface, which returns the type directly, removing the need for casting.
See msdn for implemented interfaces.