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

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

问题描述

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

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




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



解决方案

您需要转换为 IEnumerable< DataGridViewRow> DataGridViewRowCollection 只实现 IEnumerable

 code> MyDatagrid.Rows 
.Cast< DataGridViewRow>()
.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天全站免登陆