C# 从dataGrid(位于表单1)中的文本框(位于表单2中)搜索值
我想从dGVPlan(dataGrid,位于frPlanMain.cs中)中的searchTBoxW(SearchWindow.cs)中搜索一个值。 frPlanMain.cs中的dataGrid通过在locTBox中输入文件的路径来加载.xls文件,我不使用SQL。 我尝试了几种方法来实现这一点,但它们似乎不起作用,我对VisualStudio和C#还不熟悉 frPlanMain.cs的代码(格式1): SearchWindow.cs的代码(格式2): 我曾在另一篇帖子中问过类似的问题,但我无法修改代码,也无法理解为什么代码不起作用:C# 从dataGrid(位于表单1)中的文本框(位于表单2中)搜索值,c#,asp.net,wpf,winforms,datagrid,C#,Asp.net,Wpf,Winforms,Datagrid,我想从dGVPlan(dataGrid,位于frPlanMain.cs中)中的searchTBoxW(SearchWindow.cs)中搜索一个值。 frPlanMain.cs中的dataGrid通过在locTBox中输入文件的路径来加载.xls文件,我不使用SQL。 我尝试了几种方法来实现这一点,但它们似乎不起作用,我对VisualStudio和C#还不熟悉 frPlanMain.cs的代码(格式1): SearchWindow.cs的代码(格式2): 我曾在另一篇帖子中问过类似的问题,但我无
非常感谢您的关注和帮助,如果我要求太多,我很抱歉。您可以将事件处理程序附加到Search表单:
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;
}
}
下一步而不是newsearchwindow().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;
}
}
公共部分类搜索窗口:表单
{
公共frPlanMain refTofrPlanMain;
public SearchWindow(frPlanMain f)//您的搜索窗口根本不起任何作用。您在该窗口中键入的搜索键是结果窗口还是结果窗口?是的,它缺少代码,我尝试了几个示例,即使我正确引用了所需的项,它们也会抛出错误。我正在尝试在SearchBox(文本框)中输入值然后通过按SearchButtonW在我的DataGridView中搜索它。我用我使用的命令更新了Form2,得到了一个未处理的异常,类型为“System.NullReferenceException”。我收到以下错误:附加信息:语法错误:在“Like”运算符之前缺少操作数。是因为我没有引用某个colu吗mn?我相信bs.Filter=“比如“%”+searchTBoxW.Text+“%”中缺少某些内容;在like之前。哦,对了,你应该指定列和字符串的那部分。没有检查代码。问题是我从某个路径导入了一个.xls,并且没有预先确定的列。或者是否可以创建一个列并告诉GridView从.xls文件中显示哪些列?你可以在MS网站上创建DataGridView读取列名(从xls的第一行)手动。答案非常简单,从.xls文件的第一行中的名称创建列,然后添加所有行。谢谢。几乎可以了,但仍然需要解决如何定义从何处获取文本的列,但它解决了如何分配列的问题。如果有与此相关的问题,我可以与您联系吗?
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;
}
}