如果int递增,则删除DGV中的行?

时间:2014-07-29 22:50:37

标签: c# mysql datagridview

我有一个方法将datagridview中的每一行保存到数据库中,但是如果保存,我想删除每一行。我有这个方法:

public static void SaveMajEq(MajorEquipment_CreateView CView)
{
    rowCount = 0;
    // For each cell in the DataGrid, stores the information in a string.
    for (rows = 0; rows < CView.dgvCreate.Rows.Count; rows++)
    {
        if (CView.dgvCreate.Rows[rows].Cells[col].Value != null)
        {
            // Creates a model, then populates each field from the cells in the table.
            MeModel = new MajorEquipment_Model();
            MeModel.EquipmentNumber = Convert.ToString(CView.dgvCreate.Rows[rows].Cells[0].Value);
            MeModel.EquipmentType = Convert.ToString(CView.dgvCreate.Rows[rows].Cells[1].Value);
            MeModel.Location = Convert.ToString(CView.dgvCreate.Rows[rows].Cells[2].Value);
            MeModel.Notes = Convert.ToString(CView.dgvCreate.Rows[rows].Cells[3].Value);

            Database_Facade.Operation_Switch(OPWRITE);
        }
        CView.dgvCreate.Rows.RemoveAt(rows);
    }
    MessageBox.Show(rowCount + " Entries stored in database.");
}

rowCount在单独的类中以try/catch方法递增,因此如果发生任何事情,它将不会递增。我想要做的只是实现这一行:

CView.dgvCreate.Rows.RemoveAt(rows);

仅在rowCount每次递增时才会增加。我不确定如何实现此功能。

1 个答案:

答案 0 :(得分:1)

恢复循环顺序

for (rows = CView.dgvCreate.Rows.Count - 1; rows >= 0 ; rows--)

向后循环允许删除循环处理的行,而不会影响要处理的下一行的索引。