0

按键时如何逐个搜索网格中的单元格c#

我尝试了key press event几次,但没有奏效。

有一个例外。

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsLetter(e.KeyChar))
    {
         for (int i = 0; i < (dataGridView1.Rows.Count); i++)
         {
             if (dataGridView1.Rows[i].Cells[0].Value.ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
             {
                 list.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
                 if (dataGridView1.Rows[i].Cells[0].Value.ToString() == list[i].ToString())
                 {
                     dataGridView1.Rows[i].Cells[0].Selected = true;
                 }

                 //dataGridView1.Rows[i].Cells[0].Selected = true;
                 /*if (dataGridView1.Rows[i].Cells[0].Selected == true) 
                 {
                      dataGridView1.Rows[i].Cells[0].Selected = false;
                 } */

                 // stop looping
              }
          }              
    }            
}
4

2 回答 2

0

终于找到了答案。它会这样解决。

    private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Char.IsLetter(e.KeyChar))
        {
            for (int i = 0; i < (dataGridView1.Rows.Count); i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
                {
                    if (lastKey == '\0')
                    {
                        lastKey = e.KeyChar;
                        if (lastKey == e.KeyChar && lastIndex < i)
                        {
                            int last = lastIndex;
                            lastIndex = i;
                            lastKey = e.KeyChar;
                            this.clearGrid(last);
                            dataGridView1.Rows[i].Cells[0].Selected = true;
                            return;
                        }
                        else
                        {
                            continue;
                        }

                    }
                    else
                    {

                        if (lastKey != e.KeyChar)
                        {
                            int last = lastIndex;
                            lastIndex = i;
                            lastKey = e.KeyChar;
                            this.clearGrid(last);
                            dataGridView1.Rows[i].Cells[0].Selected = true;
                            return;


                        }
                        else
                        {
                            // int last = lastIndex;
                            if (lastKey == e.KeyChar && lastIndex < i)
                            {
                                int last = lastIndex;
                                lastIndex = i;
                                lastKey = e.KeyChar;
                                this.clearGrid(last);
                                dataGridView1.Rows[i].Cells[0].Selected = true;
                                return;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                else
                {

                    continue;


                }

            }
        }
    }

    private void clearGrid(int l)
    {
        dataGridView1.Rows[l].Cells[0].Selected = false;
    }
于 2013-07-19T03:29:21.960 回答
0

此行没有意义,也可能导致IndexOutOfBoundException ArgumentOutOfRangeException,因为并非每一行都可以添加到列表中,因此i可能大于列表计数。

if (dataGridView1.Rows[i].Cells[0].Value.ToString() == list[i].ToString())

删除列表,它应该可以工作:

private int  lastIndex;
private char lastKey;

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsLetter(e.KeyChar))
    {
        for (int i = 0; i < (dataGridView1.Rows.Count); i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value.ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
            {
                 if (lastKey == e.KeyChar && lastIndex < i)
                 {
                     continue;
                 }

                 lastKey = e.KeyChar;
                 lastIndex = i;
                 dataGridView1.Rows[i].Cells[0].Selected = true;
                 return;
            }
        }               
    }
}
于 2013-07-18T05:41:27.903 回答