比较两个DataGridView并通过匹配列C#返回DataGridView



c#windowsforms。
Proyect:导入2 Excel,然后选择保存表的表格,然后在两个文件中比较列"代码",然后在第三个DataGridView上显示匹配的列。

DGV1                
Code    Date        Tipe    Price   Account
3367    02-feb-18   NEW     25      N/A
8543    04-feb-18   NEW     25      N/A
3367    05-feb-18   RENEW   50      N/A
5542    07-feb-18   NEW     75      N/A
1069    27-jan-18   NEW     25      N/A
DGV2                
City    Code
Texas   3367
Texas   8543
Texas   5542
Texas   8673            
DGV3                
Code    Date        Tipe    Price   Account
3367    02-feb-18   NEW     25      N/A
3367    05-feb-18   RENEW   50      N/A
8543    04-feb-18   NEW     25      N/A
5542    07-feb-18   NEW     75      N/A

目前正在使用本指南:
https://forgetcode.com/csharp/1508-comparing-two-datatables-and-andurning-a-datable------------a-databable-matching-on-more-more-columns-linq#但是我还没有明白。希望有人可以通过解释来帮助我剩下的部分。

private void btnSelect1_Click(object sender, EventArgs e)
{
     try
     {
          OpenFileDialog openfiledialog1 = new OpenFileDialog();
          openfiledialog1.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
          if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
               this.tBox1.Text = openfiledialog1.FileName;
          }
          string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox1.Text + ";Extended Properties = "Excel 12.0; HDR=YES;" ; ";
          OleDbConnection con = new OleDbConnection(constr);
          con.Open();
          dropdown_sheet1.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
          dropdown_sheet1.DisplayMember = "TABLE_NAME";
          dropdown_sheet1.ValueMember = "TABLE_NAME";
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }
}
public void btnLoad1_Click(object sender, EventArgs e)
{
     try
     {
          string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox1.Text + ";Extended Properties = "Excel 12.0; HDR=YES;" ; ";
          OleDbConnection con = new OleDbConnection(constr);
          OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + dropdown_sheet1.SelectedValue + "]", con);
          DataTable dt1 = new DataTable();
          sda.Fill(dt1);
          foreach (DataRow row in dt1.Rows)
          {
               dataGridView1.DataSource = dt1;
          }
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }
}
private void btnSelect2_Click(object sender, EventArgs e)
{
     try
     {
          OpenFileDialog openfiledialog2 = new OpenFileDialog();
          openfiledialog2.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
          if (openfiledialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
               this.tBox2.Text = openfiledialog2.FileName;
          }
          string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox2.Text + ";Extended Properties = "Excel 12.0; HDR=YES;" ; ";
          OleDbConnection con = new OleDbConnection(constr);
          con.Open();
          dropdown_sheet2.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
          dropdown_sheet2.DisplayMember = "TABLE_NAME";
          dropdown_sheet2.ValueMember = "TABLE_NAME";
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }
}
public void btnLoad2_Click(object sender, EventArgs e)
{
     try
     {
          string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox2.Text + ";Extended Properties = "Excel 12.0; HDR=YES;" ; ";
          OleDbConnection con = new OleDbConnection(constr);
          OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + dropdown_sheet2.SelectedValue + "]", con);
          DataTable dt2 = new DataTable();
          sda.Fill(dt2);
          foreach (DataRow row in dt2.Rows)
          {
               dataGridView2.DataSource = dt2;
          }
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }
}

我已经进行了导入和工作表的选择,但是我不理解比较。

获取两个数据网格作为数据表的数据。

public DataTable getLinq(DataTable dt1, DataTable dt2)
        {
            DataTable dtMerged = (from a in dt1.AsEnumerable()
                                  join b in dt2.AsEnumerable()
                                  on a["code"].ToString() equals b["code"].ToString()
                                  into g
                                  where g.Count() > 0
                                  select a).CopyToDataTable();
            return dtsimilar;
        }`
       set dtsimilar as data source for  third grid

相关内容

  • 没有找到相关文章

最新更新