Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete selected cells' rows windows form
    primarykey
    data
    text
    <p>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...</p> <p>Some problem may occurs: Index out of bound, delete the whole gridview...</p> <p>let's assume that the gridview content 2 columns, data in gridview is not from db.</p> <p>I've tried this code: </p> <pre><code>while(dgv1.selectedCells.count&gt;0){ dgv1.removeAt(dgv1.selectedCells[0].RowIndex); } </code></pre> <p>but this one delete the whole datagridview, since when gridview remove one row, it get the first cell (first col, first row) selected.</p> <p>then I try this:</p> <pre><code>List&lt;int&gt; rowToDelete = new List&lt;int&gt;(); foreach(DataGridViewCell cell in dgv1.selectedCells){ rowToDelete.add(cell.RowIndex); } </code></pre> <p>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.</p> <p>Edited: As yatrix recommended, I use this code (I think I got his recommended right)</p> <pre><code>for (int i = dataGridView1.SelectedCells.Count -1; i &gt;=0; i--) { dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[i].RowIndex); } </code></pre> <p>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. </p> <p>Ps: I'm trying to make a program to rename multiple files in windows.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload