C# 是否可以使DataGridViewRow.Cells支持任何或全部方法?

C# 是否可以使DataGridViewRow.Cells支持任何或全部方法?,c#,datagridview,lambda,C#,Datagridview,Lambda,比如说,我想从DataGridView检查行中是否有任何单元格的值为null。使用foreach的代码可以工作 foreach (DataGridViewCell c in row.Cells) { if (c.Value == null) { e.Cancel = true; MessageBox.Show("empty cell");

比如说,我想从
DataGridView
检查
行中是否有任何
单元格
null
。使用
foreach
的代码可以工作

        foreach (DataGridViewCell c in row.Cells)
        { 
            if (c.Value == null)
            {
                e.Cancel = true;
                MessageBox.Show("empty cell");
            }
        }
我尝试了方法
Any
来替换
foreach
,但没有编译:

        if(row.Cells.Any(c => c.Value == null))
        {
            e.Cancel = true;
            MessageBox.Show("empty cell");
        }
是否有任何方法可以使其支持方法
Any

你必须投下他们:

if (row.Cells.Cast<DataGridViewCell>().Any(c => c.Value == null)) {
  // code...
}
if(row.Cells.Cast().Any(c=>c.Value==null)){
//代码。。。
}
DataGridViewCellCollection未指定泛型类型,因此需要强制转换。见:

Cast(IEnumerable)方法通过提供必要的类型信息,允许在非泛型集合上调用标准查询运算符。例如,ArrayList不实现IEnumerable,但通过对ArrayList对象调用Cast(IEnumerable),可以使用标准查询运算符查询序列


嗨,谢谢你的回答。您的代码运行正常。我注意到
DataGridViewCellCollection
实现了接口
IEnumerable
。你能再解释一下为什么仍然需要这样的演员阵容吗?