类型' System.Data.EvaluateException'的未处理异常发生在System.Data.dll;附加信息:找不到列[]

时间:2016-03-30 08:43:27

标签: c#

我创建了两种形式:

表格1包含:

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) ***//Loads data from a certain path in GridView***
        {
            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);
            dGVPlan.DataSource = dt;
            new SearchWindow(this).Show();
            this.Show();
         }
}
}

表格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) 
        {
            refTofrPlanMain = f;
            InitializeComponent();
        }

        private void SearchButtonW_Click(object sender, EventArgs e) //Searches the value in textbox in Form2 in dataGridView in Form1.
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = refTofrPlanMain.dGVPlan.DataSource;
            bs.Filter = "[NrFir] like '%" + searchTBoxW.Text + "%'"; //<-- Error located here.
            refTofrPlanMain.dGVPlan.DataSource = bs;
        }
    }
}

当我点击搜索按钮时,我收到以下错误

  

未处理的类型&#39; System.Data.EvaluateException&#39;发生在System.Data.dll中;   附加信息:找不到列[NrFir]。

我在DataGridView中创建了一个从.xls文件中反馈到F1-F22的列。 NrFir是我想要搜索的列。

如何解决错误???

2 个答案:

答案 0 :(得分:1)

问题是您使用的DataTable作为数据源,而不是列名。所以,它正在获得自动列名。实际上,来自工作表的列名称假定为数据行。所以,你需要在这里进行清理:

    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();
        //this.Show(); why you are calling show() here?
    }

    private void CleanupDataTable(DataTable dt) {
        if(dt.Rows.Count == 0)
            return;
        var headerRow = dt.Rows[0];
        var columns = headerRow.ItemArray;
        // 1. give the right column names
        for(int i = 0, l = columns.Length; i < l; i++)
            dt.Columns[i].ColumnName = columns[i].ToString();
        // 2. remove the header row from the result
        dt.Rows.Remove(headerRow);
    }

更新:更多详情:

在xls文件中创建标题并将其加载到DataTable时,xls中的标题行也不会是DataTabe中的标题。因此,您的DataTable标题将为F1,F2,....因此,如果您希望DataTable具有与xls文件相同的标题,则必须手动执行此操作。因此,您需要按照以下步骤操作:

  1. 将xls文件加载到DataTable
  2. 获取DataTable的第一行(实际上是标题行)
  3. 从第一行读取值并逐列编辑DataTable的标题名称
  4. DataTable
  5. 中删除标题行

答案 1 :(得分:0)

感谢您的帮助Javad_Amiry。我发现问题出在我身边。这是因为我从数据网格的设计视图中添加了列。 当我按照DataTable中的代码添加列时,过滤器在NrFir列上工作。