如何使用 LINQ 查找 DataGridView 行? [英] How can I use LINQ to find a DataGridView row?

查看:54
本文介绍了如何使用 LINQ 查找 DataGridView 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 LINQ 样式查询来查找 DataGridView 行?我试图找到绑定到特定对象的那个并突出显示它.

MyDatagrid.Rows.FirstOrDefault(r => r.DataBoundItem == myItem).Selected = true;

<块引用>

错误 1 ​​'System.Windows.Forms.DataGridViewRowCollection' 不包含 'FirstOrDefault' 的定义,并且找不到接受类型为 'System.Windows.Forms.DataGridViewRowCollection' 的第一个参数的扩展方法 'FirstOrDefault'(是您是否缺少 using 指令或程序集引用?)

解决方案

You need to cast to IEnumerable 因为DataGridViewRowCollection 只实现了 IEnumerable代码>:

MyDatagrid.Rows.Cast().FirstOrDefault(r => r.DataBoundItem == myItem).Selected = true;

Is there any way to use a LINQ style query to find a DataGridView row? I am trying to find the one bound to a specific object and highlight it.

MyDatagrid.Rows.FirstOrDefault(r => r.DataBoundItem == myItem).Selected = true;

Error 1 'System.Windows.Forms.DataGridViewRowCollection' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Windows.Forms.DataGridViewRowCollection' could be found (are you missing a using directive or an assembly reference?)

解决方案

You need to cast to IEnumerable<DataGridViewRow> since DataGridViewRowCollection only implements IEnumerable:

MyDatagrid.Rows
    .Cast<DataGridViewRow>()
    .FirstOrDefault(r => r.DataBoundItem == myItem).Selected = true;

这篇关于如何使用 LINQ 查找 DataGridView 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆