Is it possible to delete the first 5 rows of a datatable?
是否可以删除数据表的前5行?
I tried the below but it deleted every other line.
我尝试了下面但它删除了所有其他行。
For j as interfere = 0 to lastrow step 1
Dt.rows.remove (dt.rows (j))
Next
2 个解决方案
#1
3
You can use such code, to remove first 5 rows:
您可以使用此类代码删除前5行:
For index = 1 To 5
If (dt.Rows.Count > 0) Then
dt.Rows.RemoveAt(0)
End If
Next
The if part checks if there is any row to delete.
if部分检查是否有要删除的行。
#2
1
Try it in reverse so that you don't affect the previous row
反向尝试,以免影响前一行
For j as integer = 4 to 0 step -1
Dt.rows.remove(dt.rows(j))
Next
What you're running into is in the first iteration index 0 is removed and the iterator variable becomes 1
您遇到的是在第一次迭代中,索引0被删除,迭代器变量变为1
in the second iteration what was originally at index 1 is now index 0 in the dt so your are removing index 1 and leaving what was originally 1.
在第二次迭代中,最初在索引1处的内容现在是dt中的索引0,因此您将删除索引1并保留原来的1。
#1
3
You can use such code, to remove first 5 rows:
您可以使用此类代码删除前5行:
For index = 1 To 5
If (dt.Rows.Count > 0) Then
dt.Rows.RemoveAt(0)
End If
Next
The if part checks if there is any row to delete.
if部分检查是否有要删除的行。
#2
1
Try it in reverse so that you don't affect the previous row
反向尝试,以免影响前一行
For j as integer = 4 to 0 step -1
Dt.rows.remove(dt.rows(j))
Next
What you're running into is in the first iteration index 0 is removed and the iterator variable becomes 1
您遇到的是在第一次迭代中,索引0被删除,迭代器变量变为1
in the second iteration what was originally at index 1 is now index 0 in the dt so your are removing index 1 and leaving what was originally 1.
在第二次迭代中,最初在索引1处的内容现在是dt中的索引0,因此您将删除索引1并保留原来的1。