I created two forms:
Form 1 contains:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace Plan_de_lucru_1._0 { public partial class frPlanMain : Form { public SearchWindow frm2; public frPlanMain() { InitializeComponent(); } private void frPlanMain_Load(object sender, EventArgs e) { } private void GoButton_Click(object sender, EventArgs e) { string constr = "Provider = MicroSoft.Jet.OLEDB.4.0; Data Source=" + locTBox.Text + ";Extended Properties =\"Excel 8.0; HDR=NO;\";"; OleDbConnection con = new OleDbConnection(constr); OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + shTBox.Text + "$]", con); DataTable dt = new DataTable(); sda.Fill(dt); CleanupDataTable(dt); // call the cleanup here, before binding dgv to dt dGVPlan.DataSource = dt; new SearchWindow(this).Show(); } private void CleanupDataTable(DataTable dt) { if(dt.Rows.Count == 0) return; var headerRow = dt.Rows[0]; var columns = headerRow.ItemArray; for (int i = 0, l = columns.Length; i < l; i++) dt.Columns[i].Namespace = columns[i].ToString(); dt.Rows.Remove(headerRow); } } }
And Form 2 :
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Plan_de_lucru_1._0 { public partial class SearchWindow : Form { public frPlanMain refTofrPlanMain; public SearchWindow(frPlanMain f) //<<Edit made here { refTofrPlanMain = f; InitializeComponent(); } private void SearchButtonW_Click(object sender, EventArgs e) { BindingSource bs = new BindingSource(); bs.DataSource = refTofrPlanMain.dGVPlan.DataSource; bs.Filter = "NrFir like '%" + searchTBoxW.Text + "%'"; refTofrPlanMain.dGVPlan.DataSource = bs; } } }
When i click on the search button I receive the following error
An unhandled exception of type 'System.Data.EvaluateException' occurred in System.Data.dll; Additional information: Cannot find column [NrFir].
I have created columns in the DataGridView that referances to F1-F22 from a .xls file. NrFir is the column that i want to search in.
How can i fix the error ???