Click here to Skip to main content
15,066,928 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team,

While selecting multiple rows in Datagridview in vb.net some rows not deleting.
when I delete event (KeyDown)occur. See below,

Example:
DataGrid having 100 rows.

For item As Integer = 0 To DataGrid.RowCount - 1 Step 1
                    If DataGrid.Rows(item).Selected Then
                    Logic code here... 
                    ---------
                    ---------
                    End If
Next
I am selecting 5 rows in DataGrid and press delete key,
Now only 4 or less rows deleting
I can not able to know remaining selected rows why not deleting.
Please help me.
Posted
Updated 9-Jul-14 2:08am
v2

If you think about that datagridview that your are stepping through using
SQL
For item As Integer = 0 To DataGrid.RowCount - 1 Step 1

Try to picture it with row numbers ...
0. Line 0 selected
1. Line 1 selected
2. Line 2 selected
3. Line 3 selected
4. Line 4 selected

Imagine we are going through your loop ... start with item = 0 - your code deletes row 0 ... before we go any further let's look at the grid again ... it doesn't look like this
0. Line 0 selected
1. Line 1 selected
2. Line 2 selected
3. Line 3 selected
4. Line 4 selected
we've deleted row 0 so it now looks like this when we add it the row numbers
0. Line 1 selected
1. Line 2 selected
2. Line 3 selected
3. Line 4 selected
item now moves on to a value of 1 so your code deletes row 1 as the grid now stands NOT what was in row 1 when you started.

To get around this, start your loop at the end of the datagridview and work backwards - that way the row numbers that you haven't got to yet are the same as they were when you started.

For item As Integer = DataGrid.RowCount - 1 To 0 Step -1
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900