DataGridView中删除行只删除隔行 [英] DataGridView Removing Rows removes only alternate rows
问题描述
我有一个DataGridView 100行。然后我删除的每一行,如下图所示,但因为它可以将周围的ID即将在为0,2,4,6,8,因此,其只能去除偶数行。这是怎么回事?
的foreach(的DataGridViewRow行dgvData.Rows)
{
尝试
{
如果(row.IsNewRow)
继续;
串PalletID = row.Cells [1] .Value.ToString();
字符串位置= row.Cells [2] .Value.ToString();
dgvData.Rows.Remove(行);
AddToList(PalletID +设在+位置+上传);
}
赶上(例外前)
{
的MessageBox.show(错误上传数据);
AddToList(错误上传数据+ ex.Message);
继续;
}
}
我相信你遍历网格,并删除当前行,它搞乱与截至目前会导致行到网格的索引移的含量最高。所以,当你再继续下一排它已经搬到了指数,其中你删除的行。
您最好的选择是使用for循环并运行它反转,这应该迎刃而解。
I have 100 rows in a DataGridView. I then remove each row as shown below but as it loops around the ID is coming in as 0,2,4,6,8 and therefore its removing only even rows. What's going on?
foreach (DataGridViewRow row in dgvData.Rows)
{
try
{
if (row.IsNewRow)
continue;
string PalletID = row.Cells[1].Value.ToString();
string Location = row.Cells[2].Value.ToString();
dgvData.Rows.Remove(row);
AddToList(PalletID + " located in " + Location + " was uploaded");
}
catch (Exception ex)
{
MessageBox.Show("Error Uploading Data");
AddToList("Error uploading data " + ex.Message);
continue;
}
}
I believe as you loop through the grid and delete the current row, its messing up with the index of the grid which as of now would be causing the rows to shift a level up. So as you proceed further to the next row it has already moved to the index wherein you deleted the row.
Your best bet would be use a for loop and run it reverse and this should work out fine.
这篇关于DataGridView中删除行只删除隔行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!