Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Datagrid that gets rows from a database table.
I wrote code to populate a textbox when double clicking a row.
If I doubleclick a row all is ok and no errors
but if i doubleclick the area below the last row it gives me an error.
This is in VB / VB.net
The code to load the rows into the Grid is as below
The table (MyTable) has 3 records only

Private Sub LoadMyGrid(ByVal TypeID As Long)
        Dim myConn As New OleDbConnection
        myConn = Provider.OleConnectDB
        Dim rReader As OleDbDataReader
        Dim dt As New DataTable
        Dim myCmd As New OleDbCommand("Select * From MyTable", myConn)
        rReader = myCmd.ExecuteReader
        dt.Load(rReader)

        With MatGrid
            .DataSource = dt
            .AutoGenerateColumns = True
        End With
        rReader.Close()
        myCon
End Sub

Private Sub MyGrid_DoubleClick(sender As Object, e As EventArgs) Handles MyGrid.DoubleClick
	txtName.text=MyGrid.CurrentRow.Cells(1).Value
    End Sub


What I have tried:

i have tried to trap the error with Try/Catch but doesn't work

Looks to me that when i doubleclick on the area of the grid below the 3 rows VB doesn't know the row index for some reason
Posted
Updated 15-Jan-24 7:38am
v3
Comments
Jo_vb.net 14-Jan-24 10:17am    
1. Please show your code for the click event
2. Which error message appears when clicking on next row?
3. Are you using WPF or WinForms?
[no name] 15-Jan-24 13:52pm    
You intercepted a "parent" click; and then expect the "child" properties to somehow be in sync. Expecting a valid "CurrentRow" (without checking) ... when you did not "click" a row, etc. ... is "unrealistic".

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

We have no idea what the error message is, what the code that generates it is, and no way to duplicate your problem as a result - which means we can't help you fix the problem.

So, it's going to be up to you.

Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the double click handler, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

At a guess, you are trying to use a row or column index that doesn't exist for the blank area, and getting an "index out of range" error as a result. The solution for that is simple - find out which index is out of range, what it actually is, and modify your code to check for it being valid before you try to use it.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Thank you i found the solution

I was using the event DoubleCLick but in stead i have to use the event CellMouseDoubleClick

Thank you so much, much appreciated
 
Share this answer
 
Comments
Maciej Los 15-Jan-24 14:58pm    
This is not an answer. Please, delete it or provide proper solution.

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