-1

I have a datagridview where i have implemented a search function. By entering characters while datagridview is in focus the first row of the grid with the characters will be selected.

I use:

dtgView[index].Selected = true;
dtgView.FirstDisplayedScrollingRowIndex = index;

The row gets selected, but when i press the up or down arrows to navigate up or down from the selected row, the datagridview starts from row index 0 in the datagrid and not the newly selected row?

Here is the OP's Original Code / Method

private void dtgView_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyValue >= 65 && e.KeyValue <= 90 )
    {
        searchStrings += e.KeyCode;
        for (int i = 0; i < dtgView.RowCount; i++)
        {
            if (dtgView.Rows[i].Cells[0].Value.ToString().
                Substring(0, searchStrings.Length) == searchStrings)
            {
                dtgView.ClearSelection();
                dtgView.FirstDisplayedScrollingRowIndex = i;
                dtgView.Rows[i].Selected = true;
                dtgView.Rows[i].Cells[0].Selected = true;
                break;
            }
        }
    }

}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
duta
  • 1
  • 2
  • `Duta` perhaps you can show the full Method block(s) that pertain to the Events..also make sure that you don't have any other events that will be triggered based on a buttons key press.. 2 lines of code really doesn't do anyone any justice here... – MethodMan Dec 05 '14 at 21:04
  • What `Keys are you wanting to Use I think that you should check on there is a better way to do this – MethodMan Dec 05 '14 at 21:13
  • maybe so but this doesnt solve my problem with the selected row. – duta Dec 05 '14 at 21:14
  • the kays are a-z by the way :-) – duta Dec 05 '14 at 21:15
  • I think that you need to change your code in the `if statement to the following `if ((e.Key >= Key.A) || (e.Key <= Key.Z)){ e.Handled = true}` also have you actually debugged this code..? what the heck does this do `searchStrings += e.KeyCode;` and why don't you use a Textbox or something to do some searching and then rebind the data grid anyway I think you need to use e.Key instead of e.KeyCode personally – MethodMan Dec 05 '14 at 21:25
  • searchString is the string which i use to compare the row text to know which row i am selecting. Yes your suggestion about rethinking the use of keys are right, but that still doesnt solve my problem. – duta Dec 05 '14 at 21:28
  • you haven't solved your problem because you haven't stated whether you have debugged the code or not.. I bet if you step thru the code you will see where your pitfalls are happening.. good luck – MethodMan Dec 05 '14 at 21:31
  • Yes of course i have debugged my code. The row gets selected, if the row is outside the viewport the datagridview scrolls down to the selected row. But still when i press the down array the datagridview jumps up to row index 0 and goes to row index 1. – duta Dec 05 '14 at 21:34
  • So what is the `KeyValue` when you press the Down Arrow..? also I think you need to assign `e.Handled = true` as well after you break out of that code.. perhaps the keyvalue is with in the range of 65 and 90 – MethodMan Dec 05 '14 at 21:36
  • take a look at another option http://edn.embarcadero.com/article/30129 – MethodMan Dec 05 '14 at 21:40
  • The kayvalue for down is 40. Setting e.Handled = true does not help either... – duta Dec 05 '14 at 21:41
  • maybe you need to switch it to a different event perhaps `KeyPress or KeyDown` – MethodMan Dec 05 '14 at 21:42
  • Great reading on the article DJ KRAZE, but this is not my problem. My problem is concerned with the selection of datagridview row. – duta Dec 05 '14 at 21:44
  • I am out of here.. sorry.. it's the weekend perhaps you should rethink / refactor your code – MethodMan Dec 05 '14 at 21:48

1 Answers1

0

This is my Key_up event on the dtg:

private void dtgView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue >= 65 && e.KeyValue <= 90 )
            {
                searchStrings += e.KeyCode;
                for (int i = 0; i < dtgView.RowCount; i++)
                {
                    if (dtgView.Rows[i].Cells[0].Value.ToString().
                        Substring(0, searchStrings.Length) == searchStrings)
                    {

                        dtgView.ClearSelection();
                        dtgView.FirstDisplayedScrollingRowIndex = i;
                        dtgView.Rows[i].Selected = true;
                        dtgView.Rows[i].Cells[0].Selected = true;

                        break;
                    }
                }
            }

        }
duta
  • 1
  • 2