如果在每个签名中,则立即发送 [英] Immediate If in For Each signature
问题描述
我的表格有2个网格.选项卡控件的每个选项卡页上的一个.
My form has 2 grids. One on each tab page of a tab control.
在选择哪个选项卡页之前,我想遍历网格1中的所有行或仅迭代网格2中的选定行.但是我想对两个网格使用相同的代码.
Pending on which tab page is selected, I want to iterate over all of the rows in grid 1 or only selected rows in grid 2. But I want to use the same code for both grids.
这是我的代码:
DataGridView d;
if (tabEmails.SelectedIndex == 0)
{
d = grid1;
}
else
{
d = grid2;
}
foreach (DataGridViewRow r in tabEmails.SelectedIndex == 0 ? d.Rows : d.SelectedRows)
{
// do stuff
}
除了,它不会编译.
关于如何解决这个问题的任何想法?
Any ideas on how to get round this ?
J
jppnn
推荐答案
如果让我们知道编译错误是什么以及它在哪里发生,那就太好了.
It is really nice if you let us know what the compile error is and where it occurs.
如果我不得不猜测,问题是d.Rows是DataGridViewRowCollection,而d.SelectedRows是DataGridViewSelectedRowCollection,因此?:运算符无法确定表达式的结果类型. 不知道为什么他们需要两个不同的东西 属性的类型.
If I had to take a guess, the issue is that d.Rows is a DataGridViewRowCollection and d.SelectedRows is a DataGridViewSelectedRowCollection and so the ?: operator cannot determine the result type of the expression. No idea why they needed two different types for the properties.
它看起来真的很老,并且没有实现IEnumeable< T> ;,而只是实现了IEnumerable.
It also looks like it is really old school and doesn't implement IEnumeable<T>, just IEnumerable.
您可以执行以下操作来解决该问题:
You might be able to work around it by doing something like:
IEnumerable items;
if (tabEmails.SelectedIndex == 0)
{
items = grid1.Rows;
}
else
{
items = grid2.SelectedRows;
}
foreach (DataGridViewRow r in items)
{
// do stuff
}
这篇关于如果在每个签名中,则立即发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!