Click here to Skip to main content
13,510,830 members
Rate this:
Please Sign up or sign in to vote.
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 13-Aug-17 0:13am
Updated 13-Aug-17 2:05am
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try DataGridView.SelectedRows(0) instead.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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
End Try
End Sub

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 13 Aug 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100