Click here to Skip to main content
15,868,034 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My Datagridview has only two rows. I have set allowusertoaddnewrow = false. I am filling rows in datagridview from database. I want to transfer its value in a textbox on KeyDown Event by pressing Enter Key. When I Press Enter key in datagridview to select the contents of first row, the datagridview.currentrow.index is returned 1. In the same way when I select the second row and press Enter Key then same happens here, means it also returns the datagridview.currentrow.index = 1. Is there any concept so that I can deal with this problem.

What I have tried:

RowIndx = Me.DataGridView1.CurrentRow.Index
Posted
Updated 13-Aug-17 2:05am
Comments
Graeme_Grant 13-Aug-17 7:30am    
I don't understand what you are trying to do. Where is the TextBox located - on the form or in-cell editing? Is the Enter key press for the TextBox or the DataGrid? What are you trying to achieve when the Enter key is pressed?

What exactly is the problem that you are having?

Please click on the "Improve question" widget and give a clear and concise explanation of what you are trying to do and the problem that you have. Also, we can't see the code on your screen from here - so you need to post that too in your question.
k0satyam 13-Aug-17 8:13am    
I have placed textbox control on the form.
On pressing Enter key over the datagridview, contents of currentrow are to be transfered in the text boxes.

Try DataGridView.SelectedRows(0) instead.
 
Share this answer
 
Thanks for reply.

I had tried that also, but this couldn't helped me.

I got solution by using code :


Private Sub DataGridView1_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

Dim DataGridView1 As DataGridView = CType(e.Control, DataGridView)
AddHandler DataGridView1.PreviewKeyDown, AddressOf DataGridView1_PreviewKeyDown

End Sub

Private Sub DataGridView1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles DataGridView1.PreviewKeyDown
Dim TBox As TextBox
Dim RowNum As Integer
Try
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
TBox = Me.Owner.ActiveControl
RowNum = Me.DataGridView1.CurrentRow.Index
Call DGridToText(TBox, RowNum)
End If
Catch ex As Exception
Err.Description
End Try
End Sub
 
Share this answer
 

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