Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > putting focus on a particular cell in datagridview when user presses enter key
 

putting focus on a particular cell in datagridview when user presses enter key

iam using a editable datagridview so when the user enter s the values into the cell and when he presses enter key the focus should go to the 2nd column i have tried the following code

private void dataGridView1_KeyPress(object sender, KeyEventArgs e)

{

if (e.KeyValue == 13 && dataGridView1.Rows.Count > 0)

{

dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1];

}

}

this works fine as long as the cell edit is finished and it has no focus

i.e when the user types in the data in cell and when he tabs to next cell and when he presses enter key then the focus will go to the 2nd column but when he finishes typing and focus is on the cell when he hits enter key the above event is not getting fired

so i have tried the above in "dataGridView1_KeyUp" event this works fine but

when the user is in 5th column and hits enter it first goes to 5 th coloumn in the next row and then it goes to the 2nd column i dont want that behaviour so in which event i have to write so that whne the user hits enter key i goes directly to the 2nd column in the new row

thanks in advance

chaitu2  Wednesday, May 07, 2008 5:01 PM

Hi Chaitu,

Based on my experience, by default, DataGridView would always move the focus to next row when you press “Enter key�

So, if you want to make your Enter key acting like a Tab key, I would like to suggest you manually override DagaGirdView.ProcessDialogKey method. It’s used for processing key event when you press any key on keyboard.

In ProcessDialogKey, you can set the CurrentCell’s index as you like.

I would like to suggest you refer to our FAQ of “Windows Forms Data Controls and Databinding�/a> Forum, where there is an answer about “How can I make the Enter key works as Tab key?�/font>

For more discussion about this question, please refer to the following pages:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1437503&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1864004&SiteID=1

Hope it helps.

If there is anything unclear, please feel free to let me know.

Best wishes,

Jun Wang

Jun Wang Tim  Tuesday, May 13, 2008 9:20 AM

Hi Chaitu,

Based on my experience, by default, DataGridView would always move the focus to next row when you press “Enter key�

So, if you want to make your Enter key acting like a Tab key, I would like to suggest you manually override DagaGirdView.ProcessDialogKey method. It’s used for processing key event when you press any key on keyboard.

In ProcessDialogKey, you can set the CurrentCell’s index as you like.

I would like to suggest you refer to our FAQ of “Windows Forms Data Controls and Databinding�/a> Forum, where there is an answer about “How can I make the Enter key works as Tab key?�/font>

For more discussion about this question, please refer to the following pages:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1437503&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1864004&SiteID=1

Hope it helps.

If there is anything unclear, please feel free to let me know.

Best wishes,

Jun Wang

Jun Wang Tim  Tuesday, May 13, 2008 9:20 AM
Thanks for your tips.
But I don't know where to put the override methods, I am using vb2005 of Visual Studio (where is the Datagridview class?) (sorry about this newbie question)



Tigerwood2006  Wednesday, May 14, 2008 12:19 AM

Hi Tigerwood2006,

Usually, we create DataGridView by dragging a DataGridView control from ToolView into our Form.

But in your case, we need to manually create a inheritated custom DataGridView, let’s call it MyDataGridView. It means you have to define a new class called MyDataGridView which inheretated from DataGridView.

There are two main steps to achieve this goal:

1. Define a custom class MyDataGridView, inheretated from DataGridView. In MyDataDridView, you need not change any other code except method ProcessDialogKey and method OnKeyDown. Here is the code:

Code Snippet

Class myDataGridView

Inherits DataGridView

'

' Other code in your datagridview class.

'

Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean

If keyData = Keys.Enter Then

Dim col As Integer = Me.CurrentCell.ColumnIndex

Dim row As Integer = Me.CurrentCell.RowIndex

If row <> Me.NewRowIndex Then

If col = (Me.Columns.Count - 1) Then

col = -1

row += 1

End If

Me.CurrentCell = Me(col + 1, row)

End If

Return True

End If

Return MyBase.ProcessDialogKey(keyData)

End Function

Protected Overloads Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)

If e.KeyData = Keys.Enter Then

Dim col As Integer = Me.CurrentCell.ColumnIndex

Dim row As Integer = Me.CurrentCell.RowIndex

If row <> Me.NewRowIndex Then

If col = (Me.Columns.Count - 1) Then

col = -1

row += 1

End If

Me.CurrentCell = Me(col + 1, row)

End If

e.Handled = True

End If

MyBase.OnKeyDown(e)

End Sub

End Class

Jun Wang Tim  Thursday, May 15, 2008 2:57 AM

2. Assign object myDataGridView to reference in Form1. Assign myDataGridView’s object to Form1 in Form1’s constructor. Here is the code:

Code Snippet

Public Sub New()

InitializeComponent()

myDataGridView = New MyDataGridView()

End Sub

Now, you can use your myDataGridView in Form1 to achieve the key down effect.

Hope it helps.

Finally, I would like to hear from you how your problem is going on. If there is anything unclear, please feel free to let me know.

Thanks.

Best wishes,

Jun Wang

Jun Wang Tim  Thursday, May 15, 2008 2:59 AM
i need to use the above.... but i don't know where i need to put the mydatagridview and how to use it.

I have crated mydatagridview class

how can i make datagridview1 working when enter pressed by using this.

Help pls



snaveen  Thursday, August 06, 2009 12:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Help regarding populating gridview
• DataGridView with ComboBoxCells
• Help with Coding
• BindingList ListChanging event? Something equivalent?
• DataGrid bug: selected cell left behind on screen
• Problem with updates
• How to automatically add child rows when add parent row?
• error when making listbox datasource = hashtable
• How to use datagrid to add other controls
• How to update dataset