【问题标题】:search a DataGridView and then highlight the cell[s] in which the value is found?搜索 DataGridView,然后突出显示找到值的单元格?
【发布时间】:2012-06-30 13:43:26
【问题描述】:

我有一个 DataGridView 我正在提供一个列表

DataGridView 正在完美填充,但我现在希望能够在网格(任何列)中搜索用户输入的值。

我一直在试图潜入解决方案,但找不到 DataGridView.Cells[X,Y] 类型的属性。

我试过了:

String searchVal = textBoxValueToFindInGrid.Text.Trim();
for (int i = 0; i < dataGridViewPlatypusElements.RowCount; i++)
{
    for (int j = 0; j < dataGridViewPlatypusElements.ColumnCount; j++) {
        if (dataGridViewPlatypusElements.Text.Contains(searchVal))
        {
            dataGridViewPlatypusElements.GoTo*(Cells[j,i]);
        }
    }
}

...但是,当然,DataGridView.Text 不包含任何有用的东西。这是我需要 Cells[X,Y] 属性的地方。

  • 在这种情况下,我不会认为它有害。

【问题讨论】:

标签: c# winforms search datagridview full-text-search


【解决方案1】:

可能对有类似问题的其他人有用:

if(dataGridView.Rows[i].Cells[j].Value.ToString().Contains(searchText)
{
    dataGridView.Rows[i].Cells[j].Selected = true;
}

如果您需要有关单元格的更多信息,可以浏览选定的单元格:

foreach(DataGridViewCell selectedCell in dataGridView.SelectedCells)
{
    ..........
}

【讨论】:

    【解决方案2】:

    怎么样

    dataGridView.Rows[X].Cells[Y].Value
    

    【讨论】:

    • Brooklynhouse:如何在找到匹配项后选择该单元格,或在找到匹配项时选择那些单元格(可能一次只能选择一个单元格)?
    猜你喜欢
    • 2014-10-05
    • 2012-03-24
    • 2022-11-03
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 2011-03-27
    相关资源
    最近更新 更多