Comparing Of Two DataGridView And Delete Same Rows In C#

Comparing Of Two DataGridView And Delete Same Rows In C#

I have 2 DataGridViews. After doing some operations in my first DataGridView, I transfer the rows ??to my second DataGridView. Now, I want to do this. In my second DataGridView, I don't want to add rows that are the same as the first DataGridView.

I have 2 DataGridViews. After doing some operations in my first DataGridView, I transfer the rows ??to my second DataGridView. Now, I want to do this. In my second DataGridView, I don't want to add rows that are the same as the first DataGridView.

My DataGridView1 looks like that:

           Column 1                Column 2
       ---------------------------------------
           hello friends              250
           hi guys                    15
           good day                   15684
           old days                   156153
           bye bye                    6143

If my DataGridView2 looks like that;

           Column 1                Column 2
       ---------------------------------------
           new coders                 88
           hello friends              250
           january february           31
           such good                  1684
           play music                 1553
           bye bye                    6143

I don't want to see same rows in my DataGridView2 (from DataGridView1) and I don't want to add it. So my DataGridView2 should look like that;

           Column 1                Column 2
       ---------------------------------------
           new coders                 88
           january february           31
           such good                  1684
           play music                 1553

It's my code:

       connection.Open();
       string[] ss = listBox.SelectedItem.ToString().Split(' ');
       int css = ss.Count();
       for (int mi = 0; mi < css; mi++)
       {
           string mq = "SELECT c1, c2, c3 FROM myTable WHERE c1='" + ss[mi] + "' OR c2='" + ss[mi] + "'";
           SqlDataAdapter da = new SqlDataAdapter(mq, connection);
           DataTable dt1 = new DataTable();
           sd.Fill(dt1);
           foreach (DataRow r in dt1.Rows)
           {
               dgv1.Rows.Add(r["c1"].ToString() + " " + r["c2"].ToString(), r["c3"]);
           }
       }
       //Some insignificant operations.
       for (int mi2 = 0; mi2 < countOFMYNEWFORDGV1; mi2++)
       {
           string mySecondQuery= "SELECT c1, c2, c3 FROM myTable WHERE c1='" + myNEWFORDGV1[mi2] + "' OR c2='" + myNEWFORDGV1[mi2] + "'";
           SqlDataAdapter secondDA= new SqlDataAdapter(mySecondQuery, conection);
           DataTable dtForSecond= new DataTable();
           secondDA.Fill(dtForSecond);
           foreach (DataRow mySecondRow in dtForSecond.Rows)
           {
               dgv2.Rows.Add(mySecondRow["c1"].ToString() + " " + mySecondRow["c2"].ToString(), mySecondRow["c3"]);                        
           }
       }
       con.Close();

Note 1: I edited my code.

Note 2: Please share your full answer. Thank you so much.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

SQL Server Tutorial For Beginners - Learn MS SQL Server with Examples

SQL Server Tutorial For Beginners, Microsoft SQL Server Tutorial, SQL Server Training. Microsoft SQL Server explains all the fundamentals of MS SQL Server with examples. What is DBMS? What is SQL? What is SQL Server? SQL Server Installation

How Containerized SQL Server Makes Development Easier

Managing development databases can be tricky, especially if members of the team want to develop on a Mac or Linux device. In this presentation, we learn how running SQL Server in Docker containers can help ensure a consistent development & testing experience for the entire team.

Python with SQL Server Database

In this article you will learn: How to connect through SQL Server Database using Python? How can we store some information into the SQL Server database using Python? How can we perform an update and delete operations? How we will retrieve stored information from the SQL Server database?