且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在datagridview vb.net中选择多行时,有些行没有删除?

更新时间:2023-12-04 13:36:58

这可能对你有所帮助......



http://***.com/questions/14563807/datagridview-multiple -row-selection-issue [ ^ ]


如果您考虑使用
 对于 item 作为整数=  0    DataGrid。 RowCount   -   1 步骤 1  



尝试用行号来描绘它......

 0。第0行选择
1.第1行选择
2.第2行选择
3.第3行选择
4.选择第4行



想象一下,我们正在完成你的循环...以 item = 0 开头 - 你的代码删除第0行...在我们再进一步看之前让我们看一下再次网格... 看起来像这样

  0。选择第0行 
1.第1行选择
2.第2行选择
3.第3行选择
4.选择第4行

我们已经删除了第0行,所以当我们添加行号时,它现在看起来像这样。

 0。第1行选择
1.第2行选择
2.第3行选择
3.选择第4行

item 现在移动到 1 的值,因此您的代码将删除行 1 ,因为网格现在不是您启动时第1行中的内容。



为了解决这个问题,在datagridview的末尾开始循环并向后工作 - 那样你还没有得到的行号是一样的就像你刚开始时一样。



对于项目As Integer = DataGrid.RowCount  -  1 To 0 Step -1 


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.

May be this will help you...

http://***.com/questions/14563807/datagridview-multiple-row-selection-issue[^]


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