【发布时间】:2016-07-17 08:07:32
【问题描述】:
我想从 dGVPlan(dataGrid,位于 frPlanMain.cs)中的 searchTBoxW (SearchWindow.cs) 中搜索一个值。 frPlanMain.cs 中的 dataGrid 通过在 locTBox 中输入文件的路径来加载 .xls 文件,我不为此使用 SQL。 我尝试了几种方法来做到这一点,但它们似乎不起作用,我是 Visual Studio 和 C# 的新手。
frPlanMain.cs(Form1)的代码:
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 frPlanMain()
{
public SearchWindow frm2;
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=Yes;\";";
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().Show();
this.Show();
}
}
}
SearchWindow.cs(Form2)的代码:
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()
{
InitializeComponent();
}
private void SearchButtonW_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = refTofrPlanMain.dGVPlan.DataSource;
bs.Filter = " like '%" + searchTBoxW.Text + "%'";
refTofrPlanMain.dGVPlan.DataSource = bs;
}
}
}
我在另一篇文章中问过这个类似的问题,但我无法修改代码或理解它为什么不起作用:Search value in textBox so it finds it in the data located in gridView
非常感谢您的关注和提供的帮助,如果我问的太多,我很抱歉。
【问题讨论】:
-
您的搜索窗口根本不执行任何操作。它是您输入搜索键的窗口还是结果窗口?
-
是的,它缺少代码,我尝试了几个示例,即使我正确引用了所需的项目,它们也会给我带来错误。我正在尝试在 searchTBox (文本框)中输入一个值,然后按 SearchButtonW 在我的 DataGridView 中搜索它。
-
我用我使用的命令更新了 Form2,我得到一个未处理的“System.NullReferenceException”类型的异常发生。
标签: c# asp.net wpf winforms datagrid