【问题标题】:Comparing two datagridview and returning a datagridview by matching a column C#比较两个datagridview并通过匹配列C#返回一个datagridview
【发布时间】:2019-05-05 04:24:38
【问题描述】:

C# Windows 窗体。
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-returning-a-datatable-by-matching-one-or-more-columns-using-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);
     }
}

我已经有了导入和工作表选择,但我不明白比较。

【问题讨论】:

    标签: c# excel import datagridview compare


    【解决方案1】:

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

    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
    

    【讨论】:

    • 我得到了两个数据表的数据,但是当我尝试比较时,它在 datagridview 上没有显示任何结果
    • 你能告诉我你在哪里调用这个函数的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多