【问题标题】:Combine two queries in one datagridview在一个数据网格视图中组合两个查询
【发布时间】:2018-08-28 09:21:36
【问题描述】:

我的应用通过 openfiledialog 连接到 excel 文件。我有两个搜索,主要搜索和次要搜索。我想在一个 datagridview 中得到他们的结果。我的代码(主要搜索):

private void searchbtn_Click(object sender, EventArgs e)
{
    try
    {
        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        OleDbConnection conn = new OleDbConnection(connStr);
        OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$] where [" + comboBox1.SelectedItem.ToString() + "] = '" + textBox5.Text + "'", conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView2.DataSource = dt;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

和二次搜索:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        OleDbConnection conn = new OleDbConnection(connStr);
        OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$] where [" + addcb.SelectedItem.ToString() + "] = '" + addtb.Text + "'", conn);
        DataTable ds = new DataTable();
        da.Fill(ds);
        dataGridView2.DataSource = ds;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

有什么想法吗?

【问题讨论】:

标签: c# sql excel oledb


【解决方案1】:

您可以拥有 2 个数据表,比如说 dt1dt2 和一个合并的 dtAll。然后您可以合并 2 并将其设置为 DataSource

private UpdateDataSource()
{
    dataGridView2.Rows.Clear();
    dataGridView2.Refresh();
    dtAll.Clear();

    if(dt1 == null && dt2 != null)
    {
        dtAll = dt2;
    }
    else if(dt2 == null && dt1 != null)
    {
        dtAll = dt1;
    }
    else if(dt1 != null && dt2 != null)
    {
        dtAll = dt1.Copy();
        dtAll.Merge(dt2);
    }
    else
    {
        dtAll = null;
    }
    dataGridView2.DataSource = dtAll;
}

您的事件处理程序应该是这样的:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        OleDbConnection conn = new OleDbConnection(connStr);
        OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$] where [" + addcb.SelectedItem.ToString() + "] = '" + addtb.Text + "'", conn);
        dt2.Clear();
        da.Fill(dt2);
        UpdateDataSource();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

【讨论】:

    【解决方案2】:

    在二级搜索中,而不是:

    dataGridView2.DataSource = ds;
    

    试试:

    DataTable combinedData = (DataTable)(dataGridView2.DataSource);
    combinedData.Merge(ds);
    dataGridView2.DataSource = combinedData;
    

    您可能还必须注意避免在新数据源中出现重复。另外:我没有测试这个解决方案,它只是一个想法。

    希望对你有帮助

    【讨论】:

    • 非常感谢!它有帮助!
    【解决方案3】:

    您需要使用merge。有它的教程here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多