Ben
Ben

Reputation: 2523

Remove rows in datagridview

I have a method that stores each line in a gridview into the database, then if the save is successful, removes the row; but if it isn't successful (cannot be stored in the db) it does not remove the row. Unfortunately, I can't get the row-removal to work properly.

This is my current code:

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
    }
}

I have also tried:

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

But that crashes at halfway, because the Rows index keeps changing each time a row is removed.

How can I achieve this? How can I remove a row if the save is successful, but keep it if there is an error?

Upvotes: 0

Views: 150

Answers (3)

Vizard
Vizard

Reputation: 303

You have to sort removeRows in descending order.

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

Then use the foreach loop

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

This way the reindexing wont affect the deletion.

Upvotes: 1

frankie
frankie

Reputation: 728

Try to populate collection of rows for removing with DataGridViewRow not with index. This works for me.

    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);
        }
    }

Upvotes: 1

yogesh
yogesh

Reputation: 574

May this help:

1] Make sure correctSave is being modified correctly.

2] Revert the loop flow, Looping backward allow to remove the row processed by the loop without affecting the index of the next row to process.

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

3] Use CView.dataGridView1.Rows.RemoveAt(rows);

Upvotes: 1

Related Questions