如何在插入时从datagridview中删除重复的行? [英] How can I delete duplicate rows from datagridview on insert?

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

问题描述

你好,



我有一个关于我的datagridview的问题。对于我的datagridview我正在使用数据表和数据集。我有2个相关的sql查询填写dg.One是

 string query =select * from RegisterStudent p inner join StudentCourses c on p.SN = c.StudentId where c.CourseId ='+ cmbClassId.SelectedValue.ToString() +'; 

whre cmbClassId是学生的courseID。这个查询从RegisterStudent表中获取特定于该课程的所有学生,并将其显示在dg.Now中,这是我这样做是因为我希望有一个列表显示该课程的所有学生。当我必须在另一个名为AttendanceList的表中插入这些值和其他值时出现问题。我这样做:

 var query = 
插入AttendanceList(SN,sNr,fName,lName,dateArrival,Attending,Departed,CourseId)值(@ SN,@ sNr,@ fName,@ lName,@ dateArrival,@ Attending,@出发,@ ClassId);

对于它们两者都很完美,但我的问题是:如何在插入AttendanceList中的值时删除dg中的重复行?我应该通过查询还是相关的dg?如果它与dg有关,我怎么能这样做才能删除重复的行?我提到我已经尝试了所有可能的选择,但没有任何运气。谢谢你提前!



我尝试过:



如何从c#中的datagridview中删除重复的行? - 堆栈溢出 [ ^ ]

https://www.c-sharpcorner.com/forums/remove-duplicates-from-datagridview-c-sharp

删除数据网格视图中的重复行 [ ^ ]

https://www.codeproject .com / Articles / 36697 /从网格视图中消除重复值

https://www.aspsnippets.com/Articles/Remove-Delete-Duplicate-Rows-Records- from-DataTable-using-C-and-VBNet.aspx

我也试过这个:

 int rowIndex = 0; 
if(!this.dg.Rows [rowIndex] .IsNewRow)
{
this.dg.Rows.RemoveAt(rowIndex);
}



当我有rowIndex = 0时,它将删除索引为0的行,如果它与select语句中的行相同。在returen中我想从select中删除一个。如果我改为rowIndex = dg.Rows.Count,它会给我一个超出范围异常的索引。

解决方案

在同一时间将数据绑定到数据表时执行一项操作或删除重复行以下示例请检查。







从表中删除重复记录 [ ^ ]

Hello,

I have a question regarding my datagridview.For my datagridview I'm using a datatable and a dataset.I have 2 sql queries that are relevant for filling the dg.One is "

string query = " select * from RegisterStudent p inner join StudentCourses c on p.SN = c.StudentId where c.CourseId = '" + cmbClassId.SelectedValue.ToString() + "'";

whre cmbClassId is the courseID of the student.This query takes all the students from RegisterStudent table specific to that course and it displays it in dg.Now,this I am doing because i would like to have a list displayed with all the students for that course.My problem comes when I have to insert these values plus others in another table called AttendanceList.I'm doing it like this:"

var query =
                                  "Insert into AttendanceList(SN,sNr,fName,lName,dateArrival,Attending,Departed,CourseId)Values(@SN,@sNr,@fName,@lName,@dateArrival,@Attending,@Departed,@ClassId) ";

For both of them it works perfectly,but my question is:How can I delete the duplicate rows in the dg on inserting the values in AttendanceList?Should i do it through a query or relating the dg?And if it is related to the dg,how can i do it so that it will remove duplicate rows?I mention I have tried all the possible choices for this but without any luck.Thank you in advance!

What I have tried:

How to remove duplicate row from datagridview in c#? - Stack Overflow[^]
https://www.c-sharpcorner.com/forums/remove-duplicates-from-datagridview-c-sharp
Remove duplicated rows in a datagridview[^]
https://www.codeproject.com/Articles/36697/Eliminate-Duplicate-Values-from-the-Grid-View
https://www.aspsnippets.com/Articles/Remove-Delete-Duplicate-Rows-Records-from-DataTable-using-C-and-VBNet.aspx
And I also have tried this:

int  rowIndex = 0;
            if (!this.dg.Rows[rowIndex].IsNewRow)
            {
                this.dg.Rows.RemoveAt(rowIndex);
            }


when I have rowIndex=0 it will delete the row that is at index 0 if it is alike to the one from the select statement.In returen i would like it to delete the one from select.If I changed to rowIndex=dg.Rows.Count it will give me an index out of range exception.

解决方案

do one thing when you binding data to datatable in that same time do or remove duplicate rows following example please check.



Remove duplicate records from a table[^]


这篇关于如何在插入时从datagridview中删除重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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