我在尝试过滤搜索结果时收到错误“找不到 [Product_Name]”

本文关键字:找不到 Product Name 错误 过滤 搜索结果 | 更新日期: 2023-09-27 18:33:36

我收到此错误,我已经检查了我的列名,它是正确的,这是我的代码段:

public partial class Search : Form
{
    private OleDbConnection connection = new OleDbConnection();
    DataTable DT = new DataTable();
    public Search()
    {
        InitializeComponent();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|'StockRecord.accdb; Persist Security Info=False;";
    }
{   
    try
    {
      connection.Open();
      DataView DV = new DataView(DT); //DT has been declared as a global variable for a DataTable.
      DV.RowFilter = String.Format("Product_Name LIKE '%{0}%'", radTextBox1.Text);
      dataGridView1.DataSource = DV;
      connection.Close();
     }
     catch (Exception S)
     {
       MessageBox.Show("" + S);
     }
}
}

我在尝试过滤搜索结果时收到错误“找不到 [Product_Name]”

你应该有这样的东西:

public partial class Search : Form
{
  private OleDbConnection connection = new OleDbConnection();
  DataTable DT = new DataTable();
  public Search()
  {
    InitializeComponent();
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|'StockRecord.accdb; Persist Security Info=False;";
    try
    {
      connection.Open();
      SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Product", connection);
      da.Fill(DT);
      DataView DV = new DataView(DT); //DT has been declared as a global variable for a DataTable.
      DV.RowFilter = String.Format("Product_Name LIKE '%{0}%'", radTextBox1.Text);
      dataGridView1.DataSource = DV;
      connection.Close();
     }
     catch (Exception S)
     {
       MessageBox.Show("" + S);
     }
  }
}

Product是表名。