Eddie
Eddie

Reputation: 1941

Delete selected cells' rows windows form

The idea is: when user select a cell or multiple cells then click remove button, Datagridview will remove the whole rows of those selected cells. My mind is blow-up when there's nothing show how to delete multiple cells rows. Problem is simple if delete selected rows or single cells. But multiple cells problem is...

Some problem may occurs: Index out of bound, delete the whole gridview...

let's assume that the gridview content 2 columns, data in gridview is not from db.

I've tried this code:

while(dgv1.selectedCells.count>0){
dgv1.removeAt(dgv1.selectedCells[0].RowIndex);
}

but this one delete the whole datagridview, since when gridview remove one row, it get the first cell (first col, first row) selected.

then I try this:

List<int> rowToDelete = new List<int>();
foreach(DataGridViewCell cell in dgv1.selectedCells){
  rowToDelete.add(cell.RowIndex);
}

then delete gridview Rows from the list, but this may occur same rows index to be added, that cause wrong row to be deleted, and may make the code more complicated. Please help.

Edited: As yatrix recommended, I use this code (I think I got his recommended right)

for (int i = dataGridView1.SelectedCells.Count -1; i >=0; i--)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[i].RowIndex);
        }

but when I select cells more than Rows.Count, index out of bound exception occur. also, if I select 2 cell in the same rows, it cause wrong row to be deleted (the next row). And that's my main problem.

Ps: I'm trying to make a program to rename multiple files in windows.

Upvotes: 2

Views: 5493

Answers (4)

brundha
brundha

Reputation: 1

for(int i=0;i<10;i++)

{

dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);

}

Upvotes: 0

Eddie
Eddie

Reputation: 1941

Anyway, I asked my teacher and here is his final answer for this code:

        var q = from c in dataGridView1.SelectedCells.Cast<DataGridViewCell>()
                orderby c.RowIndex descending
                select c.RowIndex;

        int[] arr = q.Distinct().ToArray();

        foreach (int rowindex in arr)
        {
            dataGridView1.Rows.RemoveAt(rowindex);
        }

And this one is good, yet linq is a bit hard to understand...

Upvotes: 0

Munawar
Munawar

Reputation: 2587

It does the required job, try it.

List<int> rowsToDelete = new List<int>();

 foreach (DataGridViewCell cell in this.dgv1.SelectedCells)
{
   if (rowsToDelete.Contains(cell.RowIndex) == false)
       rowsToDelete.Add(cell.RowIndex);
} 

//User may select the cells/Rows using ctrl key hence make sure to sort the row indexes in descending order.

rowsToDelete=rowsToDelete.OrderByDescending(rowIndex => rowIndex).ToList();          
foreach (Int32 rowIndex in rowsToDelete)
{
   this.dgv1.Rows.RemoveAt(rowIndex);
}

rowsToDelete.Clear();

Upvotes: 2

Yatrix
Yatrix

Reputation: 13805

You were close. Traverse backwards through the row indexes of the selected cells, that way they don't change. Don't add duplicate row indexes to your list! If you delete from the top, the rows move up and a new row takes that old index. If you delete from the bottom, you just move upward and it doesn't matter that a new item moved up because you've moved past that index.

So:

for (i = myRowIndexes.count - 1; i >= 0; i--) {
    //delete your rows
}

Upvotes: 0

Related Questions