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.