我想将 DataGridviewRow
作为 ref
传递给它的单元格,但我无法这样做
foreach(DataGridViewRow dgr in dgvMarksEntryByClassWise.Rows)
{
RowValueSet(ref dgr);
}
这里给出了编译时错误,因为 dgr 是一个foreach 迭代变量
我也尝试用 for loop
for (int i = 0; i < dgvMarksEntryByClassWise.Rows.Count; i++)
{
RowValueSet(ref dgvMarksEntryByClassWise.Rows[i]);
}
但这里也给出了编译时错误:
属性、索引器或动态成员访问不能作为 out 引用传递
我提到了针对上述错误提出的这个问题,但没有找到任何适合我的问题的解决方案
- foreach iteration variable
- A property,indexer or dynamic member acces cannot passed as an out reference
请教我怎么做
更新代码
void RowValueSet(ref DataGridViewRow dgr)
{
dgr.Cells["StudentZero"].Value = ss.Where(w => w.MarksheetMarks == "0").Count();
if (ss.Count() != 0)
dgr.Cells["StudentISEmpty"].Value = Convert.ToInt16(lblTotlatStudent1.Text) - ss.Count();
else
dgr.Cells["StudentISEmpty"].Value = 0;
dgr.Cells["StudentEntry"].Value = ss.Count();
}
最佳答案
您不能使用“foreach”甚至“for”来分配迭代的枚举项。 我绕过了一次,使用那段代码:
List<DataGridViewRow> lstDgr = new List<DataGridViewRow>();
foreach(DataGridViewRow dgr in dgvMarksEntryByClassWise.Rows)
{
DataGridViewRow dgrTemp = dgr;
RowValueSet(ref dgrTemp);
lstDgr.Add(dgrTemp);
}
dgvMarksEntryByClassWise.Rows.Clear();
dgvMarksEntryByClassWise.Rows.AddRange(lstDgr); //Not sure about the AddRange, try the Add method instead
希望对你有帮助
关于c# - 传递 DataGridViewRow 作为引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21250005/