【发布时间】:2016-03-07 10:31:23
【问题描述】:
我正在尝试从 DataGridView 中选择多行,而不是使用 for-each 循环进行迭代。
我可以使用此代码选择 1 项:
DataGridViewRow row2 =
(from DataGridViewRow r in dgView.Rows
where r.Cells["name"].Value.ToString().Equals("Akins Ford")select r).FirstOrDefault();
但是当我尝试选择多行时,使用以下代码:
List<DataGridViewRow> rows2 =
(from DataGridViewRow r in dgView.Rows
where r.Cells["status"].Value.ToString().Equals("active")select r);
我遇到了一个错误:
错误 2 无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?) C:\Software\Json\Json\Form1.cs 188 18 Json
【问题讨论】:
-
你可以试试,而不是 .FirstOrDefault() 试试 .ToList()?
-
我得到对象引用未设置为对象的实例。错误
-
你能试试这个吗....
dataGridView1.Rows.Cast<DataGridViewRow>().Where(x => x.Cells["status"].Value.ToString() == "active").ToList().ForEach(x => x.Selected = true);
标签: c# linq datagridview