【问题标题】:Search values from textBox (located in Form2) in dataGrid (located in Form1)在 dataGrid(位于 Form1 中)中从 textBox(位于 Form2 中)中搜索值
【发布时间】: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


【解决方案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(this).Show();代替new SearchWindow().Show();

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

【讨论】:

  • 我收到以下错误:附加信息:语法错误:在“喜欢”运算符之前缺少操作数。是因为我没有参考某个专栏吗?我相信 bs.Filter = " like '%" + searchTBoxW.Text + "%'" 中缺少一些东西;在喜欢之前。
  • 哦,对了,您应该指定列和字符串的那部分。没有检查代码。
  • 问题是我从某个路径导入 .xls 并且没有预先确定的列。或者是否可以创建一个列并告诉 GridView 要从 .xls 文件中显示哪些列?
  • 您可以在 MS 网站上使用 this answer 创建 DataGridView 以手动读取列名(从 xls 的第一行开始)。答案很简单,从 .xls 文件的第一行中的名称创建列,然后添加所有行。
  • 谢谢。几乎明白了,仍然需要研究如何定义从哪里获取文本的列,但它研究了如何分配列。如果我有与此相关的问题,我可以联系你吗?
猜你喜欢
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多