System.Data.dll中发生未处理的“System.Data.EvaluateException”类型异常附加信息:找不到列[Name]

时间:2016-10-16 10:14:58

标签: c# csv

我想在Visual Studio上的文本框中执行一个简单的搜索框,您可以在其中输入特定的字母或关键字,以便从.csv中搜索/过滤现有的合同

然后它将显示在dataGridView1上。我解析了.csv并从中创建了一个名为“tbl”的数据源。

现在,每次我在txtBoxSearch上输入内容时,都会显示此错误。

  

“System.Data.EvaluateException”类型的未处理异常   发生在System.Data.dll

中      

其他信息:找不到   专栏[姓名]

这些是与错误相关的部分

private void txtBoxSearch_TextChanged(object sender, EventArgs e)
{
    DataView DV = new DataView(tbl);
    DV.RowFilter = string.Format("Approved Contracts LIKE '%{0}%'", txtBoxSearch.Text);
    dataGridView1.DataSource = DV;
}

这是错误的地方 - >

DV.RowFilter = string.Format("Approved Contracts LIKE '%{0}%'", txtBoxSearch.Text);

这是我的完整代码。

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.SqlClient;




namespace Contract_Management_System
{
    public partial class frmMain : Form
    {
        DataTable tbl = new DataTable();
        string file = "Approved.csv";
        int colNum = 10;
        DataSet dataset;


        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] lines = System.IO.File.ReadAllLines(file);
            string[] data_col = null;
            int x = 0;

            foreach (string line in lines)
            {
                data_col = line.Split(',');

                if (x == 0)
                {
                    //header
                    for (int i = 0; i <= data_col.Count() - 1; i++)
                    {
                        tbl.Columns.Add(data_col[i]);
                    }
                    x++;
                }
                else
                {
                    //data
                    tbl.Rows.Add(data_col);
                }
            }
            dataGridView1.DataSource = tbl;

            DataGridViewButtonColumn createButtonColumn = new DataGridViewButtonColumn();
            createButtonColumn.Name = "Create Contract";
            createButtonColumn.Text = "Create";
            int columnIndex = 2;

            if (dataGridView1.Columns["Create Contract"] == null)
            {
                dataGridView1.Columns.Insert(columnIndex, createButtonColumn);
            }

            dataGridView1.CellClick += dataFromBAC_CellClick;

        }

        private void dataFromBAC_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["Create Contract"].Index)
            {
                //MessageBox.Show("Create!");
                //dataFromBAC.Rows.RemoveAt(this.dataFromBAC.SelectedRows[0].Index);
            }
        }

        private void txtBoxSearch_TextChanged(object sender, EventArgs e)
        {
            DataView DV = new DataView(tbl);
            DV.RowFilter = string.Format("Name LIKE '%{0}%'", txtBoxSearch.Text);
            dataGridView1.DataSource = DV;
        }

        private void btnFromBAC_Click(object sender, EventArgs e)
        {
            fromBAC form2 = new fromBAC();
            form2.Visible = true;
        }

        private void btnPending_Click(object sender, EventArgs e)
        {
            Pending form4 = new Pending();
            form4.Visible = true;
        }

        private void btnEndNotif_Click(object sender, EventArgs e)
        {
            End form3 = new End();
            form3.Visible = true;
        }

        private void btnFromAMO_Click(object sender, EventArgs e)
        {
            fromAMO form5 = new fromAMO();
            form5.Visible = true;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的完整代码&#39;块与您的样本不同 - 它具有:

DV.RowFilter = string.Format("Name LIKE '%{0}%'", txtBoxSearch.Text);

哪个匹配异常

Additional information: Cannot find column [Name]

只会有一个名为&#39; Name&#39;的列。如果csv中有一个带有标题&#39;名称&#39;的列。我建议查看csv并确认其中的标题。此外,在添加列名称时执行调试日志或转储 - 您可能会发现它们添加了引号或空格,因此数据表中的实际列名称为["Name"]或{{1} }

如果您打算过滤名为[Name ]的列,其中包含空格,则您的实际过滤行应为:

Approved Contracts