Home » VB.NetRSS

using PreviewKeyDown event in DataGridView in winform to navigate using up and down arrows

We have a winform application using Oracle DataAccessClient. There is a gridview and textboxes where we when we click on a row we can populate the textboxes to show more of the data so the user does not have to scroll horizontally.  The code that does this is in the cell click event. We want to also do this if the user presses the up or down arrow. We put the code in the PreviewKeyDown event but we are getting the row that had the focus rather than the row that has the current arrow pointing to it. We searched the web at a lot of forums but we can not seem to locate how to do it. How do we move the focus to the row the arrow is pointing to in the grid? Here is the code:

Private

 

Sub masterDataGridView_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles masterDataGridView.PreviewKeyDown

 

Dim objConnection2 AsNew OracleConnection(conStr)

objConnection2.ConnectionString = conStr

 

Dim mySQL AsString = ""

 

Try

objConnection2.Open()

 

Dim objCommand AsNew OracleCommand()

objCommand.Connection = objConnection2

 

If masterDataGridView.CurrentRow.Cells(0).Value.ToString() <> ""Then

 

Dim objRrParm AsNew OracleParameter("rrparm", OracleDbType.Varchar2)

objRrParm.Value = masterDataGridView.CurrentRow.Cells(0).Value.ToString()

objCommand.Parameters.Add(objRrParm)

mySQL =

"select * from rr_record_requests where rr_number = :rrparm"

 

'MsgBox(objRrParm.Value)

 

EndIf

objCommand.CommandText = mySQL

objCommand.CommandType = System.Data.CommandType.Text

 

Dim RR_RECORD_REQUESTSTableAdapter AsNew OracleDataAdapter(objCommand)

 

Dim objTable AsNew DataTable()

RR_RECORD_REQUESTSTableAdapter.Fill(objTable)

 

'Me.masterDataGridView.DataSource = objTable

lblTotalRows.Text =

"Total rows returned = " + objTable.Rows.Count.ToString

 

If objTable.Rows.Count = 0 Then

MessageBox.Show(

"Record request not found")

 

Else

 

Me.tbxRequestNumber.Text = Convert.ToString(objTable.Rows(0)("rr_number"))

 

Me.tbxFlName.Text = Convert.ToString(objTable.Rows(0)("requestor_fl_name"))

 

Me.tbxCompany.Text = Convert.ToString(objTable.Rows(0)("company"))

 

Me.tbxAddress.Text = Convert.ToString(objTable.Rows(0)("address"))

 

Me.tbxCity.Text = Convert.ToString(objTable.Rows(0)("city"))

 

Me.tbxState.Text = Convert.ToString(objTable.Rows(0)("state"))

 

Me.tbxZip.Text = Convert.ToString(objTable.Rows(0)("postal_code"))

 

Me.tbxTelephone.Text = Convert.ToString(objTable.Rows(0)("telephone"))

 

Me.tbxFax.Text = Convert.ToString(objTable.Rows(0)("fax"))

 

Me.tbxEmail.Text = Convert.ToString(objTable.Rows(0)("email_address"))

 

Me.tbxAgreeToPay.Text = Convert.ToString(objTable.Rows(0)("agreetopay_yn"))

 

Me.tbxRecElec.Text = Convert.ToString(objTable.Rows(0)("electronic_yn"))

 

Me.tbxElecDesc.Text = Convert.ToString(objTable.Rows(0)("electronic_desc"))

 

Me.tbxFirstName.Text = Convert.ToString(objTable.Rows(0)("first_name"))

 

Me.tbxLastName.Text = Convert.ToString(objTable.Rows(0)("last_name"))

 

Me.tbxPrevNames.Text = Convert.ToString(objTable.Rows(0)("previous_names"))

 

Me.tbxSsn.Text = Convert.ToString(objTable.Rows(0)("ssn"))

 

Me.tbxDoi.Text = Convert.ToString(objTable.Rows(0)("date_of_injury"))

 

Me.tbxDob.Text = Convert.ToString(objTable.Rows(0)("date_of_birth"))

 

Me.tbxDesc.Text = Convert.ToString(objTable.Rows(0)("description"))

 

Me.tbxOriginSrc.Text = Convert.ToString(objTable.Rows(0)("origin_source"))

 

Me.tbxOriginDate.Text = Convert.ToString(objTable.Rows(0)("origin_date"))

 

Me.tbxChangeSrc.Text = Convert.ToString(objTable.Rows(0)("change_source"))

 

Me.tbxChangeDate.Text = Convert.ToString(objTable.Rows(0)("change_date"))

 

EndIf

objConnection2.Close()

 

Catch ex As Exception

System.Windows.Forms.MessageBox.Show(ex.ToString())

 

Finally

objConnection2.Close()

 

EndTry

 

EndSub

Thanks.

Bruce

 

2 Answers Found

 

Answer 1

Hi Bmayfield,

 

Welcome to MSDN forums!

 

I modify your code layout to make your post more readable.

By the way, you can consider posting it at Windows Forms Data Controls and Databinding Forum for better support.

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/threads

Best regards,

Martin Xie

 

Answer 2

Thanks. Posted question in Windows Forms Data Controls and Databinding Forum

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter