从dataGrid中的textBox(位于Form2中)搜索值(位于Form1中)

时间:2016-03-29 16:26:02

标签: c# asp.net wpf winforms datagrid

我想在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

非常感谢您提供的兴趣和帮助,如果我问得太多,我很抱歉。

1 个答案:

答案 0 :(得分:1)

您可以将事件处理程序附加到searhc表单:

public partial class SearchWindow : Form
{
    public String SearchKey{
       get{return searchKey_textbox.Text}
    }
    public SearchWindow()
    {
        InitializeComponent();
    }

    private void SearchButtonW_Click(object sender, EventArgs e)
    {

    }

    public void searchBtn_attachClickHandler(EventHandler eh){
         searchBtn.Click += eh;
    }

}

接下来代替new SearchWindow().Show();使用以下代码:

String searchKey = "";  //This variable could be global (just so you could use the next few lines almost anywhere
SearchWindow sw = new SearchWindow(); //create a form and attach a handler that will be triggered when "search" button in search form is clicked
//Note that you only need to attach the handler (below) only when you create the form.
sw.searchBtn_attachClickHandler += delegate(object sender, EventArgs e) {
searchKey = sw.SearchKey;
sw.close //will close after getting the search key, remove this line if you don't want it
};
sw.Show(); //finally shows the form
//at this point your searchKey form should already have the value from your search form.

请注意,您可能需要重命名一些变量,我在不知道您的名字的情况下写这个。

编辑:基于您对搜索表单编辑的第二个选项 这实际上很重要,因为它是一个有效的方法,但你在构造函数中缺少一个参数。

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 = "[column name] like '%" + searchTBoxW.Text + "%'";
    refTofrPlanMain.dGVPlan.DataSource = bs;
    }

}

而不是new SearchWindow().Show();使用new SearchWindow(this).Show();

长话短说这个答案的第二部分将您的主要表单作为参数传递,以便可以对其中的数据进行更改。你保留了所有的逻辑,但没有通过表格。