在DataGridView中搜索匹配或部分匹配
本文关键字:DataGridView 搜索 | 更新日期: 2023-09-27 18:04:56
尝试筛选谷歌搜索结果;没有运气。我要做的是,在一个文本框中给定一些文本:搜索至少在我的DataGridView的任何列的部分匹配,并有控制选择第一行(显示它作为顶行,并有它突出显示),它遇到的部分匹配。
下面是我对DataGridView的数据源的声明,以及这些列是如何组织的:
var queryData = from va in xdoc.Descendants("language")
select new
{
StringID = va.Parent.Parent.Attribute("id").Value,
Language = va.Attribute("name").Value,
LanguageData = va.Element("value").Value,
};
var organizedData = from x in queryData
group x by x.StringID into xg
select new
{
StringID = xg.Key,
English = xg.SingleOrDefault(x => x.Language == "ENGLISH_US").LanguageData,
Custom = xg.SingleOrDefault(x => x.Language == languageBox.SelectedItem.ToString()).LanguageData,
};
mainView.DataSource = organizedData.ToList();
下面是处理点击"Search"按钮的函数的当前定义:
private void searchButton_Click(object sender, EventArgs e)
{
int currentIndex = mainView.CurrentRow.Index;
if (searchBox.Text.Length == 0)
{
mainView.CurrentCell = mainView[0,0];
mainView.Focus();
return;
}
}
像这样的东西可能会让你接近:
string searchForText = "whatever";
DataGridViewRow rowFound = mainView.Rows.OfType<DataGridViewRow>()
.FirstOrDefault(row => row.Cells.OfType<DataGridViewCell>()
.Any(cell => ((dynamic)cell.Value).StringID.Contains(searchForText)));
if (rowFound != null) {
mainView.Rows[rowFound.Index].Selected = true;
mainView.FirstDisplayedScrollingRowIndex = rowFound.Index;
}