c# - 如何删除重复数据,包括datagridview vb.net中的原始数据
问题描述
如何删除 datagridview 中的重复数据和原始数据?我尝试了不同的代码,但我只设法删除了重复项。
Dim i As Integer = 0
While i < numberOfRows
For ii As Integer = (numberOfRows) To (i + 1) Step -1
If dtg3.Rows(i).Cells(0).Value.ToString() =
dtg3.Rows(ii).Cells(0).Value.ToString() And
dtg3.Rows(i).Cells(2).Value.ToString() =
dtg3.Rows(ii).Cells(2).Value.ToString() And
dtg3.Rows(i).Cells(3).Value.ToString() =
dtg3.Rows(ii).Cells(3).Value.ToString() And
dtg3.Rows(i).Cells(8).Value.ToString() =
dtg3.Rows(ii).Cells(8).Value.ToString() Then
dtg3.Rows.Remove(dtg3.Rows(ii))
numberOfRows -= 1
End If
Next
i += 1
End While
我想要的是,从我的 datagridview 中删除重复数据和原始数据,希望有人在这里帮助我。
解决方案
你去吧
For Each row As DataGridViewRow In DataGridView1.Rows
For Each nextrow As DataGridViewRow In DataGridView1.Rows
If row.Index <> nextrow.Index Then
If row.Cells(0).Value = nextrow.Cells(0).Value Then
MsgBox("Duplicate on col 0, index = " & row.Index.ToString)
End If
If row.Cells(2).Value = nextrow.Cells(2).Value Then
MsgBox("Duplicate on col 2, index = " & row.Index.ToString)
End If
If row.Cells(3).Value = nextrow.Cells(3).Value Then
MsgBox("Duplicate on col 3, index = " & row.Index.ToString)
End If
If row.Cells(8).Value = nextrow.Cells(8).Value Then
MsgBox("Duplicate on col 8, index = " & row.Index.ToString)
End If
End If
Next
Next
这还将检查所有列的重复项,不仅像在您的示例中那样在它下面的行,您正在递增 i 和递增 ii 所以您总是同时只检查 2 行而不是将 1 行与所有其他行进行比较..
推荐阅读
- latitude-longitude - 从给定的公里计算纬度和经度
- reactjs - 使用 npm run build 后如何查看对我的 create-react-app 的编辑?
- node.js - 在 nodeJS 中按任务运行 ansible playbook
- complexity-theory - 运行时复杂度:对每行、列和对角线进行排序的二维数组进行排序?
- java - Java Intstream 获取具有空值的多维数组的索引
- php - 将php表单输入写入txt文件
- python - 在 Python 中从控制台获取异步输入
- python - 无法从 /media/ 文件夹中获取图像,如果我尝试通过它的索引访问它就可以了。模板内的 for 循环不起作用
- android - Expo - Intent.getExtras() 上的 LocationTaskService.java NullPointerException
- html - 子 div 中的内容与父 div 重叠