搜索DataGridView进行匹配或部分匹配 [英] Searching a DataGridView for a match or partial match

查看:110
本文介绍了搜索DataGridView进行匹配或部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图筛选谷歌的结果;没有运气。我想要做的是,给出一个TextBox中的一些文本:在DataGridView的任何列中搜索至少一个部分匹配,并且控件选择第一行(将其显示为顶行并突出显示)它与该部分匹配遇到。



这是我对DataGridView的DataSource的声明以及列的组织方式:

  var queryData = from va in xdoc.Descendants(language)
选择新
{
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();

这里是处理搜索按钮点击的函数的当前定义: / p>

  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 =((dynamic)cell.Value).StringID.Contains(searchForText)));

if(rowFound!= null){
mainView.Rows [rowFound.Index] .Selected = true;
mainView.FirstDisplayedScrollingRowIndex = rowFound.Index;
}


Tried to sift through the google results; no luck. What I'm trying to do is, given some text in a TextBox: search for at least a partial match in any column of my DataGridView, and have the control select the first row (show it as the top row and have it highlighted) that it encounters with that partial match.

Here is my declaration of the DataSource for the DataGridView and how the columns are organized:

 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();

And here is the current definition of the function that handles a click of the 'Search' button:

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;
        }
    }

解决方案

Something like this might get you close:

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;
}

这篇关于搜索DataGridView进行匹配或部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆