Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
can we up and down record in grid view using up and down arrow key without focusing or selecting the record..

I tried many bt cant find the solution..pls give me the proper code
Posted
Updated 29-Feb-16 22:25pm
v3
Comments
Dave Kreskowiak 24-Dec-13 1:19am    
Proper code for what?? First, no one is going to write your code for you. Second, what EXACTLY do you mean by "up and down record"?? Are you talking about moving record in the datatable you bound the grid to??
kavita painter 24-Dec-13 1:29am    
i have one form..there is two control first is textbox and second is datagridview...cursor focus is always on textbox..bt when user press up and down arrow key that time data move like up and down in datagridview but that time cursor also focus on textbox....

it is possible?

Yes, but you need to handle the keydown or keypress event for the textbox.

Example: keydown event for the textbox.
private void TextBox_KeyDown(object sender, KeyEventArgs e) {
     if (sender == textBox1) { //TextBox
          //Adjust Indexes
          if (e.KeyCode == Keys.Down) {
               int c = dataGridView1.CurrentCell.ColumnIndex;
               int r = dataGridView1.CurrentCell.RowIndex;
               if (r < dataGridView1.Rows.Count-1) //check for index out of range
                    dataGridView1.CurrentCell = dataGridView1[c, r + 1];
          }
          if (e.KeyCode == Keys.Up) {
               int c = dataGridView1.CurrentCell.ColumnIndex;
               int r = dataGridView1.CurrentCell.RowIndex;
               if(r > 0) //check for index out of range
                    dataGridView1.CurrentCell = dataGridView1[c, r - 1];
          }
     }
}
 
Share this answer
 
v4
Comments
kavita painter 25-Dec-13 1:32am    
thank you so much...it works 4 me..;-)
kavita painter 25-Dec-13 1:48am    
bt there is one problem when i enter on selected rows that time it fetch the all data on another form...bt this time it not fetch bcoz focus is on textbox...
kavita painter 25-Dec-13 2:18am    
hi nw it solve i can fetch the data...bt i have another query...when i write any word for search in textbox that time it serch from grid and focus on that grid row..pls give me suggestion..
Sorry in advance. I'm not clear on what you are trying to do. So I made a genaric search for the datagrid, and may not be what you are looking for.

Sample (this will search (loop) all cells in the datagrid and return the first match):
private void TextBox_KeyDown(object sender, KeyEventArgs e) {
     if (sender == textBox1) { //TextBox
          //Search for... on Enter key, 
          //this can be any key allowed by the textbox
          if (e.KeyCode == Keys.Enter) {

               //check for no search data
               if (textBox1.Text != string.Empty || textBox1.Text != "") {
                    //get search for data
                    DataGridCell cell = SearchRecords(textBox1.Text); 

                    //check for valid cell, Not (-1,-1)
                    if(cell.ColumnNumber !=-1 && cell.RowNumber!= -1)
                         FocusOnEntry(cell);
               }
          }
     }
}

private DataGridCell SearchRecords(object data) {
     //Searches All Entries & Returns first matching

     //Get Counts for validation
     int c = dataGridView1.Columns.Count;
     int r = dataGridView1.Rows.Count;
 
     if (c != 0 && r != 0) { //Check if has Columns & Rows
          //Search Columns Left > Right & Rows Top to Bottom
          for (int col = 0; col < c; col++) {
               for (int row = 0; row < r; row++) {
                    //Check Values for Match
                    DataGridViewCell cell = dataGridView1[col, row];
                    if (cell.Value != null && cell.Value.ToString() == data.ToString())
                         return new DataGridCell(row, col); //found
               }
          }
     }
     return new DataGridCell(-1, -1); //not found
}

private void FocusOnEntry(DataGridCell index) {
     //Select the Row & Focus
     dataGridView1.Rows[index.RowNumber].Selected = true;
     dataGridView1.Focus();
}


Let me know if this is what you are looking for.
 
Share this answer
 
Comments
kavita painter 26-Dec-13 1:45am    
this is worked for me bt at search time when i pres up/down key it not work preperly...
smithjdst 26-Dec-13 5:35am    
By not working properly on up/down what do you mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900