Click here to Skip to main content
14,640,670 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Rate this:
Please Sign up or sign in to vote.

Solution 2

If you think about that datagridview that your are stepping through using
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100