My app connects to excel files via openfiledialog. I have two searches, primary search and secondary. I would like to get their result in one datagridview. My code (primary search):
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);
}
}
And secondary search:
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);
}
}
Any ideas?
In secondary search, instead of:
dataGridView2.DataSource = ds;
Try:
DataTable combinedData = (DataTable)(dataGridView2.DataSource);
combinedData.Merge(ds);
dataGridView2.DataSource = combinedData;
You might also have to take care of avoiding duplicates in your new datasource. Also: I did not test this solution, its just an idea.
Hope it helps