MOUSE CLICK AT DATAGRIDVIEW PROBLEM

Hi All,

Please see below code.

When user click outside the datagridview then error occured.

What should I do to trap it ?

Thank you.



Private Sub dgvData_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvData.MouseClick

        If dgvData Is Nothing Then Exit Sub
        If dgvData.Rows.Count = 0 Then Exit Sub

        If dgvData.SelectedRows(0).Index = -1 Then Exit Sub

        Me.Isi_Data_Mentah_From_SQL(dgvData.CurrentRow.Cells("ID").Value, _
                                    dgvData.CurrentRow.Cells("ShiftCode").Value)

    End Sub

Open in new window

LVL 1
emi_sastraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

klakkasCommented:
Check if you have at least one selected row before doing anything.

If dgvData.SelectedRows.Count = 0 then exit sub

If the selectedRows.count is zero, then dgvData.SelectedRows(0).Index will cause an error.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emi_sastraAuthor Commented:
Please see my code, I have done it.

Thank you.
emi_sastraAuthor Commented:
Sorry, yes, you are right.

Thank you very much for your help.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Jorge PaulinoIT Pro/DeveloperCommented:
Check in dgvData.CurrentRow is nothing before
klakkasCommented:
No my friend, you are not doing it...

Your code:
        If dgvData Is Nothing Then Exit Sub
        If dgvData.Rows.Count = 0 Then Exit Sub
        If dgvData.SelectedRows(0).Index = -1 Then Exit Sub

The first line is "useless": The dgvData is not nothing, since it was the sender of the event.
The second line does not check for SelectedRows, it checks for Rows.
The third line is kind of "useless". The SelectedRows array only contains valid rows.

You should change your code to this:

Private Sub dgvData_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvData.MouseClick

        If dgvData.SelectedRows.Count = 0 then exit sub

        Me.Isi_Data_Mentah_From_SQL(dgvData.CurrentRow.Cells("ID").Value, _
                                    dgvData.CurrentRow.Cells("ShiftCode").Value)

End sub
emi_sastraAuthor Commented:
You are right.

Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.