Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在DataGridView中创建类似excel的搜索?_C#_Excel_Datagridview_Find_Search Engine - Fatal编程技术网

C# 如何在DataGridView中创建类似excel的搜索?

C# 如何在DataGridView中创建类似excel的搜索?,c#,excel,datagridview,find,search-engine,C#,Excel,Datagridview,Find,Search Engine,我用这段代码在DataGridView中搜索,找到并选择一行(无过滤器)!但是,当DataGridView的行中有重复值时,它将不会获得下一行!每次单击Btn\u find(查找类似于Excel)如何转到下一行 这个想法: 向类中添加一个私有字段作为索引,以记住上次找到的行 如果搜索文本已更改,请重置此索引。(可选) 迭代所有行,从上次成功搜索后的行开始。如果上次搜索未找到结果,请从头开始 实施: private int searchIndex = -1; private void but

我用这段代码在
DataGridView
中搜索,找到并选择一行(无过滤器)!但是,当
DataGridView
的行中有重复值时,它将不会获得下一行!每次单击
Btn\u find
(查找类似于Excel)如何转到下一行

这个想法:

  • 向类中添加一个私有字段作为索引,以记住上次找到的行
  • 如果搜索文本已更改,请重置此索引。(可选)
  • 迭代所有行,从上次成功搜索后的行开始。如果上次搜索未找到结果,请从头开始
实施:

private int searchIndex = -1;

private void button1_Click(object sender, EventArgs e)
{
  button1.Text = "Find Next";

  for (int i = 0; i < dataGridView1.Rows.Count; i++)
  {
    searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
    DataGridViewRow row = dataGridView1.Rows[searchIndex];

    if (row.Cells["Foo"].Value == null)
    {
      continue;
    }
    if (row.Cells["Foo"].Value.ToString().Trim() == textBox1.Text)
    {
      dataGridView1.CurrentCell = row.Cells["Foo"];
      dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
      return;
    }
  }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
  searchIndex = -1;
}
private int searchIndex=-1;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
按钮1.Text=“查找下一步”;
对于(int i=0;i

为什么会有这些变化

  • for(int i=0;i

    • 仍然遍历每一行
  • searchIndex=(searchIndex+1)%dataGridView1.Rows.Count

    • 从第[searchIndex+1]行开始。当我们到达最后一行时,mod(
      %
      )将我们返回到第一行
  • private void textBox1\u TextChanged(对象发送者,事件参数e)
    {
    searchIndex=-1;
    }

    • 输入新的搜索条件时,从列表的开始处重新开始。这是可选的

  • 结果是如何进入列表框的。。。类似的Ecxel查找!您的意思是像Excel中的
    查找所有
    按钮一样,在按钮下方的
    查找和替换
    消息框中列出所有匹配项?这是一个单独的问题,但我想我们可以解决。是的!当然没有替换。。。类似于这张图片:@ghasemdeh虽然这最好是一个新问题,但我已经更新了我的答案,加入了
    查找所有
    功能。。。问题:
    private int searchIndex = -1;
    
    private void button1_Click(object sender, EventArgs e)
    {
      button1.Text = "Find Next";
    
      for (int i = 0; i < dataGridView1.Rows.Count; i++)
      {
        searchIndex = (searchIndex + 1) % dataGridView1.Rows.Count;
        DataGridViewRow row = dataGridView1.Rows[searchIndex];
    
        if (row.Cells["Foo"].Value == null)
        {
          continue;
        }
        if (row.Cells["Foo"].Value.ToString().Trim() == textBox1.Text)
        {
          dataGridView1.CurrentCell = row.Cells["Foo"];
          dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[row.Index].Index;
          return;
        }
      }
    }
    
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      searchIndex = -1;
    }