删除datagridview中的行

时间:2014-08-28 08:15:49

标签: c# winforms datagridview

我有一个方法将gridview中的每一行存储到数据库中,然后如果保存成功,则删除该行;但如果不成功(无法存储在数据库中),则不会删除该行。不幸的是,我无法让行删除工作正常。

这是我目前的代码:

public static void SavePAC(PlantAreaCode_CreateView CView)
{
    List<int> removeRows = new List<int>();

    // For each cell in the DataGrid, stores the information in a string.
    for (rows = 0; rows < CView.dataGridView1.Rows.Count; rows++)
    {
        correctSave = false;
        if (CView.dataGridView1.Rows[rows].Cells[col].Value != null)
        {
            // Creates a model, then populates each field from the cells in the table.
            PModel = new PlantAreaCode_Model();
            PModel.AreaCode = Convert.ToString(CView.dataGridView1.Rows[rows].Cells[0].Value);
            PModel.AreaName = Convert.ToString(CView.dataGridView1.Rows[rows].Cells[1].Value);
            PModel.Comments = Convert.ToString(CView.dataGridView1.Rows[rows].Cells[2].Value);

            // Passes the model into the Database.
            Database_Facade.Operation_Switch(OPWRITE);
        }
        if (correctSave == true) // correctSave is set in the database insert method.
        {
            removeRows.Add(rows);
        }
    }
    foreach (int i in removeRows)
    {
        CView.dataGridView1.Rows.RemoveAt(0); // Deletes all bar the last row, including any rows that cause errors
    }
}

我也尝试过:

foreach (int i in removeRows)
{
    CView.dataGridView1.Rows.RemoveAt(i);
}

但是在中途崩溃,因为每次删除行时Rows索引都会不断变化。

我怎样才能做到这一点?如果保存成功,如何删除一行,但如果出现错误则保留该行?

3 个答案:

答案 0 :(得分:1)

尝试使用DataGridViewRow而不是索引来填充要删除的行集合。这适合我。

    public void SavePAC(PlantAreaCode_CreateView CView)
    {
        List<DataGridViewRow> removeRows = new List<DataGridViewRow>();

        foreach (DataGridViewRow row in CView.dataGridView1.Rows)
        {
            correctSave = false;
            if (row.Cells[col].Value != null)
            {
                // Creates a model, then populates each field from the cells in the table.
                PModel = new PlantAreaCode_Model();
                PModel.AreaCode = Convert.ToString(row.Cells[0].Value);
                PModel.AreaName = Convert.ToString(row.Cells[1].Value);
                PModel.Comments = Convert.ToString(row.Cells[2].Value);

                // Passes the model into the Database.
                Database_Facade.Operation_Switch(OPWRITE);
            }
            if (correctSave == true) // correctSave is set in the database insert method.
            {
                removeRows.Add(row);
            }
        }

        foreach (DataGridViewRow rowToRemove in removeRows)
        {
            CView.dataGridView1.Rows.Remove(rowToRemove);
        }
    }

答案 1 :(得分:1)

愿这有帮助:

1]确保正确修改了correctSave。

2]恢复循环流,向后循环允许删除循环处理的行,而不影响要处理的下一行的索引。

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

3]使用CView.dataGridView1.Rows.RemoveAt(rows);

答案 2 :(得分:1)

您必须按降序排序removeRows。

List<int> removeRowsDesc = removeRows.OrderByDescending(i => i);

然后使用foreach循环

foreach (int i in removeRowsDesc)
{
    CView.dataGridView1.Rows.RemoveAt(i);
}

这样重建索引不会影响删除。

相关问题