我又来问问题了。我希望有人能努力回答这个问题。
所以我有一个数据 GridView ,其中包含数据库中的值。我的表单中有一个搜索按钮,可以打开一个新表单,然后有一个文本框,您可以在其中写入要搜索的字符串。问题是,如何才能搜索和更新我的 datagridview。
例如。我的数据 GridView 具有以下值:
姓名: 亚当·刘易斯, 亚当·桑德勒, 贾斯汀比伯, 丽贝卡·布莱克
如果我在单击“确定”后仅搜索“adam”,则第二个表单将关闭 我的数据 GridView 将更新,并且仅显示上面有 adam 的名称。
姓名: 亚当·刘易斯, 亚当·桑德勒
**我的数据网格的值由数据表绑定(bind)。
如果您不明白这个问题,请在下面发表评论,如果有帮助的话,我会放上我的 gui 图片。非常感谢 StackOverflow!!
看起来像这样...
最佳答案
您需要帮助的哪一部分?总体思路是这样的,您希望能够从搜索表单控制主表单。你可以这样做:
在主窗体中,您执行两件事来实现 UpdateDatatable 并在单击搜索按钮时添加事件处理程序:
private void Search_Click(object sender, EventArgs e)
{
SearchForm mySearchForm = new SearchForm();
mySearchForm.SetMainForm(this);
mySearchForm.Show();
}
public void UpdateDatatable(string searchWord)
{
//write your own code to update your datagridview by updating the datatable, filtering the datatable or creating a new datatable by using the parameter searchWord. I am saying datatable because I assume your datagridview is bound to a datatable.
}
在您的搜索表单中:
public partial class SearchForm : Form
{
private Form mainForm;
public SearchForm()
{
InitializeComponent();
}
public void SetMainForm(Form fromMainForm)
{
mainForm = fromMainForm;
}
private void txtSearchWord_TextChanged(object sender, EventArgs e)
{
mainForm.UpdateDatatable(txtSearchWord.text);
}
}
希望这有帮助
关于winforms - DatagridView 搜索 Winform - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8323131/