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

本文关键字:删除 DGV 如果 int | 更新日期: 2023-09-27 18:15:24

我有一个方法可以将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每次递增时。我不确定如何实现这个。

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

恢复循环顺序

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

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