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
。你能再解释一下为什么仍然需要这样的演员阵容吗?