Linq query has an implicit cast error for DataGridViewRow when option strict is enabled

断了今生、忘了曾经 提交于 2019-12-02 12:00:43

问题


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?


回答1:


(From _row As DataGridViewRow In dgvChanges.Rows() 

your type of the _row object has to match the single version of the collection type.

as in:

    'Assumes Option Strict On and Option Implicit On
    Dim _changes = (From _row In dgvChanges.Rows() _            
           Where Convert.ToBoolean(ctype(_row,DataGridViewRow).Cells(NAME_COLUMN_IS_SELECTED).Value) = True _           
Select DirectCast(ctype(_row,DataGridViewRow).DataBoundItem, BaseChange)).ToList()

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.



来源:https://stackoverflow.com/questions/1089059/linq-query-has-an-implicit-cast-error-for-datagridviewrow-when-option-strict-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!