如何使用winform C#从datagridview中删除重复值 [英] How to remove duplicate value from datagridview using winform C#

查看:626
本文介绍了如何使用winform C#从datagridview中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView。它包含两列,这是


I have a DataGridView. It is containing Two Columns which is

EMPId    |   DATEtiME
1        |   2019-04-20 07:30:00.000
1        |   2019-04-20 12:30:00.000
1        |   2019-04-20 13:00:00.000
2        |   2019-04-20 07:30:00.000
2        |   2019-04-20 07:30:00.000
2        |   2019-04-20 13:00:00.000
3        |   2019-04-20 07:30:00.000
3        |   2019-04-20 12:30:00.000





我想删除EMPID 1中的第二行值,我想删除第二个值在EMPID 2中,EMPID将保持不变。喜欢。





I want to remove second row value in EMPID 1 and I want to remove second value in EMPID 2 and EMPID will remain the same. Like.

EMPId    |   DATEtiME
1        |   2019-04-20 07:30:00.000
1        |   2019-04-20 13:00:00.000
2        |   2019-04-20 07:30:00.000
2        |   2019-04-20 13:00:00.000
3        |   2019-04-20 07:30:00.000
3        |   2019-04-20 12:30:00.000







请帮助任何人



我尝试过:



............ .................................................. .................................................. ..




Please help anyone

What I have tried:

..................................................................................................................

推荐答案

请注意:我不确定这是否是一个优雅的解决方案,更多的是否这可以成为一个性能瓶颈。但至少它看起来很有效。



a。)创建测试表

Please note: I'm not sure whether this is an elegant solution and more whether this can become a performance bottle neck. But at least it looks like it works.

a.) Create test table
CREATE TABLE Tbl
(
	EMPId integer,
	DATEtiME DateTime
);



b。)插入测试数据


b.) Insert Test data

INSERT INTO Tbl(EMPId, DATEtiME)
VALUES(1, '2019-04-20 07:30:00.000'),
(1, '2019-04-20 07:30:00.000'),
(1, '2019-04-20 12:30:00.000'),
(1, '2019-04-20 13:00:00.000'),
(2, '2019-04-20 07:30:00.000'),
(2, '2019-04-20 07:30:00.000'),
(2, ' 2019-04-20 13:00:00.000'),
(3, '2019-04-20 07:30:00.000'),
(3, '2019-04-20 12:30:00.000'),
(4, '2019-04-20 00:30:00.000')



c。)测试查询


c.) Test query

SELECT DISTINCT
	a.EMPId, 
	a.DATEtiME
FROM Tbl a
WHERE a.DATEtiME IN (SELECT MIN(minEmp.DATEtiME) FROM Tbl minEmp WHERE minEmp.EMPId = a.EMPId)
   OR  a.DATEtiME IN (SELECT MAX(maxEmp.DATEtiME) FROM Tbl maxEmp WHERE maxEmp.EMPId = a.EMPId)



d。)结果


d.) Result

1	2019-04-20 07:30:00.000
1	2019-04-20 13:00:00.000
2	2019-04-20 07:30:00.000
2	2019-04-20 13:00:00.000
3	2019-04-20 07:30:00.000
3	2019-04-20 12:30:00.000
4	2019-04-20 00:30:00.000





最有可能是SQL专家@ CHill60和@losmac可以给你一个更好的答案;)



Most probably the SQL experts @CHill60 and @losmac can give you a better answer ;)


选择并分组重复的列/字段。



GROUP BY EMPID,DateTime。
Select and group on the "duplicate columns / fields".

GROUP BY EMPID, DateTime.


这篇关于如何使用winform C#从datagridview中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆