C# 比较C中的两个DataGridView并删除相同的行

C# 比较C中的两个DataGridView并删除相同的行,c#,sql-server,datagridview,datatable,multiple-columns,C#,Sql Server,Datagridview,Datatable,Multiple Columns,我有两个DataGridView。在我的第一个DataGridView中执行一些操作之后,我将行??传输到我的第二个DataGridView。现在,我想这样做。在第二个DataGridView中,我不想添加与第一个DataGridView相同的行 我的DataGridView1如下所示: Column 1 Column 2 --------------------------------------- hel

我有两个DataGridView。在我的第一个DataGridView中执行一些操作之后,我将行??传输到我的第二个DataGridView。现在,我想这样做。在第二个DataGridView中,我不想添加与第一个DataGridView相同的行

我的DataGridView1如下所示:

           Column 1                Column 2
       ---------------------------------------
           hello friends              250
           hi guys                    15
           good day                   15684
           old days                   156153
           bye bye                    6143
如果我的DataGridView2看起来像这样

           Column 1                Column 2
       ---------------------------------------
           new coders                 88
           hello friends              250
           january february           31
           such good                  1684
           play music                 1553
           bye bye                    6143
           Column 1                Column 2
       ---------------------------------------
           new coders                 88
           january february           31
           such good                  1684
           play music                 1553
我不想在DataGridView1的DataGridView2中看到相同的行,也不想添加它。所以我的DataGridView2应该是这样的

           Column 1                Column 2
       ---------------------------------------
           new coders                 88
           hello friends              250
           january february           31
           such good                  1684
           play music                 1553
           bye bye                    6143
           Column 1                Column 2
       ---------------------------------------
           new coders                 88
           january february           31
           such good                  1684
           play music                 1553
这是我的密码:

       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();
注1:我编辑了我的代码


注2:请分享您的完整答案。非常感谢。

我建议不要在每一行中循环,而是在单个查询中获得结果。通过使用left-outer连接,我们将获得DataGridView2的所有结果以及DataGridView1的公共记录,然后我们将通过空值过滤器过滤掉所需的记录

GO

  ;with cte as (
    Select dg2.[Column 1] as c1, dg2.[Column 2] as c2, dg1.[Column 1] as dc1 
    from DataGridView2 as dg2 left outer join DataGridView1 as dg1 
    on dg2.[Column 1]=dg1.[Column 1] and dg2.[Column 2]=dg1.[Column 2] )
  select * from cte where dc1 is null

GO
您将仅从上述给定表结构的查询中获得所需的结果


如果有任何混淆,请ping我以获得相同的结果。

我建议在单个查询中获得结果,而不是循环遍历每一行。通过使用left-outer连接,我们将获得DataGridView2的所有结果以及DataGridView1的公共记录,然后我们将通过空值过滤器过滤掉所需的记录

GO

  ;with cte as (
    Select dg2.[Column 1] as c1, dg2.[Column 2] as c2, dg1.[Column 1] as dc1 
    from DataGridView2 as dg2 left outer join DataGridView1 as dg1 
    on dg2.[Column 1]=dg1.[Column 1] and dg2.[Column 2]=dg1.[Column 2] )
  select * from cte where dc1 is null

GO
您将仅从上述给定表结构的查询中获得所需的结果


如果有任何困惑,请与我联系。

您可以通过以下两种方法之一解决此问题

检索两个数据表中的数据并删除C代码中的重复行。 不要从数据库本身检索重复的行。 下面是我使用上面列出的方法2编写的示例代码,用于解决与您相同的问题

表和变量中的实际数据是基于您共享的代码假设的

首先,数据库中有一个表“myTable”,其中包含以下数据

   c1   |     c2    | c3   
----------------------------
 hello  |  friends  | 250
 hi     |  guys     | 15
 good   |  day      | 15684
 old    |  days     | 156153
 bye    |  bye      | 6143
 new    |  coders   | 88
 january|  february | 31
 such   |  good     | 1684
 play   |  music    | 1553
下面是在GridView2中显示数据时删除重复行的代码

var sqlConnection = new SqlConnection("Data Source=<<dbserver>>; Initial Catalog=<<dbname>>;uid=<<uid>>;password=<<password>>");

var adapter = new SqlDataAdapter();

var str = "'" + string.Join("','", ss) + "'";

// Instead of looping thru all the items of ss to query the database again and again
// I am using IN query to query the database in one opration.
string sqlstmt = "SELECT * FROM myTable where c1 IN (" + str + ") or c2 IN (" + str +")";
DataSet dataSet = new DataSet();

sqlConnection.Open();
adapter = new SqlDataAdapter(sqlstmt, sqlConnection);
adapter.Fill(dataSet);
sqlConnection.Close();
dataGridView1.DataSource = dataSet.Tables[0];

//With above code GridView1 displayed data a following.
   c1   |     c2    | c3   
----------------------------
 hello  |  friends  | 250
 hi     |  guys     | 15
 good   |  day      | 15684
 old    |  days     | 156153
 bye    |  bye      | 6143

// With following line of code I am trying to remove duplicate query parameters.
// I am doing this because Same SELECT statement is being used 
// to query the data for both GridView1 and GridView2.
// Only the parameter value changes.
myNEWFORDGV1 = myNEWFORDGV1.Except(ss).ToArray();

str = "'" + string.Join("','", myNEWFORDGV1) + "'";

string query = "SELECT * FROM myTable where c1 IN (" + str + ") or c2 IN (" + str + ")";
dataSet = new DataSet();
adapter = new SqlDataAdapter(query, sqlConnection);
adapter.Fill(dataSet);
sqlConnection.Close();
dataGridView2.DataSource = dataSet.Tables[0];

GridView2 will display following data now.
   c1   |     c2    | c3   
----------------------------
 new    |  coders   | 88
 january|  february | 31
 such   |  good     | 1684
 play   |  music    | 1553

尝试并理解此代码的逻辑,并将其应用到您的用例中。这将解决重复行的问题。

您可以通过以下两种方法之一解决此问题

检索两个数据表中的数据并删除C代码中的重复行。 不要从数据库本身检索重复的行。 下面是我使用上面列出的方法2编写的示例代码,用于解决与您相同的问题

表和变量中的实际数据是基于您共享的代码假设的

首先,数据库中有一个表“myTable”,其中包含以下数据

   c1   |     c2    | c3   
----------------------------
 hello  |  friends  | 250
 hi     |  guys     | 15
 good   |  day      | 15684
 old    |  days     | 156153
 bye    |  bye      | 6143
 new    |  coders   | 88
 january|  february | 31
 such   |  good     | 1684
 play   |  music    | 1553
下面是在GridView2中显示数据时删除重复行的代码

var sqlConnection = new SqlConnection("Data Source=<<dbserver>>; Initial Catalog=<<dbname>>;uid=<<uid>>;password=<<password>>");

var adapter = new SqlDataAdapter();

var str = "'" + string.Join("','", ss) + "'";

// Instead of looping thru all the items of ss to query the database again and again
// I am using IN query to query the database in one opration.
string sqlstmt = "SELECT * FROM myTable where c1 IN (" + str + ") or c2 IN (" + str +")";
DataSet dataSet = new DataSet();

sqlConnection.Open();
adapter = new SqlDataAdapter(sqlstmt, sqlConnection);
adapter.Fill(dataSet);
sqlConnection.Close();
dataGridView1.DataSource = dataSet.Tables[0];

//With above code GridView1 displayed data a following.
   c1   |     c2    | c3   
----------------------------
 hello  |  friends  | 250
 hi     |  guys     | 15
 good   |  day      | 15684
 old    |  days     | 156153
 bye    |  bye      | 6143

// With following line of code I am trying to remove duplicate query parameters.
// I am doing this because Same SELECT statement is being used 
// to query the data for both GridView1 and GridView2.
// Only the parameter value changes.
myNEWFORDGV1 = myNEWFORDGV1.Except(ss).ToArray();

str = "'" + string.Join("','", myNEWFORDGV1) + "'";

string query = "SELECT * FROM myTable where c1 IN (" + str + ") or c2 IN (" + str + ")";
dataSet = new DataSet();
adapter = new SqlDataAdapter(query, sqlConnection);
adapter.Fill(dataSet);
sqlConnection.Close();
dataGridView2.DataSource = dataSet.Tables[0];

GridView2 will display following data now.
   c1   |     c2    | c3   
----------------------------
 new    |  coders   | 88
 january|  february | 31
 such   |  good     | 1684
 play   |  music    | 1553

尝试并理解此代码的逻辑,并将其应用到您的用例中。这将解决重复行的问题。

从代码中可以清楚地看出,ss是一个字符串数组。您还可以共享myNEWFORDGV1的数据类型吗?在第二个for循环之前,您应该执行myNEWFORDGV1=myNEWFORDGV1.Exceptss.ToArray@ChetanRanpariya ss是字符串数组,myNEWFORDGV1是字符串。我为什么要这样做?@ChetanRanpariya我的代码没有问题。我只想做我提到的。@ChetanRanpariya我想我需要一个DataTable操作,但我不知道怎么做。从你的代码中可以清楚地看出ss是一个字符串数组。您还可以共享myNEWFORDGV1的数据类型吗?在第二个for循环之前,您应该执行myNEWFORDGV1=myNEWFORDGV1.Exceptss.ToArray@ChetanRanpariya ss是字符串数组,myNEWFORDGV1是字符串。我为什么要这样做?@ChetanRanpariya我的代码没有问题。我只想做我提到的。@ChetanRanpariya我想我需要一个DataTable操作,但我不知道怎么做。我应该在哪里添加这个代码?我不明白。我应该在哪里添加此代码?我不明白。我对这份工作太陌生了。有时我不理解复杂的结构,因为我是新手。我会试试的,谢谢你的回答,谢谢你浪费时间。我以为我需要这样的东西,但没有用;DataTable dt1=新的DataTable;DataTable dt2=新的DataTable;列表表2 Column1=dt2.AsEnumerable.Selectx=>x.FieldColumn1.ToList;DataTable dt3=dt1.AsEnumerable.Wherex=>table2Column1.IndexOfx.FieldColumn1>=0.CopyToDataTable;我对这项工作太陌生了。有时我不理解复杂的结构,因为我是新手。我会试试的,谢谢你的回答,谢谢你浪费时间。我以为我需要这样的东西,但没有用;DataTable dt1=新的DataTable;DataTable dt2=新的DataTable;列表表2 Column1=dt2.AsEnumerable.Selectx=>x.FieldColumn1.ToList;DataTable dt3=dt1.AsEnumerable.Wherex=>table2Column1.IndexOfx.FieldColumn1>=0.CopyToDataTable;