C# - 使用 foreach 和 if 语句循环遍历 DataGridView 行 - 缺少最后一个条目

问题描述

我有一个带有复选框列的 DataGridView。

enter image description here

现在我想遍历所有行,使用一个活动复选框并打印出 IdUserTrue

foreach (DataGridViewRow row in userDataGridView.Rows)
            {
                if (row.Cells[5].Value.ToString() == "True")
                {
                    MessageBox.Show(row.Cells[0].Value.ToString() + " - " + row.Cells[5].Value.ToString());
                }

            }


在本例中,IdUser 为 17 的行是最后选择的行。

我不知道为什么,但我只看到条目 15 是最后一个。 17 不再显示...

有人可以帮忙吗?


我现在尝试了以下方法

foreach (DataGridViewRow row in userDataGridView.Rows)
            {
                msg += row.Cells[0].Value.ToString() + " - " +
                     row.Cells[5].Value.ToString() + "\r\n";
            }

并且有经验,总是以某种方式无法识别带有选定复选框的最后一行。

解决方法

好的,我找到了解决方案。我加了

userDataGridView.EndEdit();

在循环之前和现在最后选择的复选框也被识别。