datakeys error

G

Guest

I have a datagrid and want to identify the primary key. Each time I try I
get an error

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name: index

The code I am using seems straight forward enough.

Private Sub DG_Glossary_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DG_Glossary.SelectedIndexChanged

Dim IDIndex As Integer
Dim IDKey As Integer
IDIndex = DG_Glossary.SelectedIndex
IDKey = DG_Glossary.DataKeys(IDIndex)
.............
end sub

IDIndex calculates fine but IDKey causes an error.
 
L

Lars-Erik Aabech

I'm not quite sure, but it might be that IDIndex is -1 if nothing has been
selected yet.
Put an if test that checs if IDIndex is >= 0 before you get the key.

HTH,
Lars-Erik
 
G

Guest

Hi Lars
Thanks for the tip but IDIndex is fine. I am using VS2003 and checked by
stepping through the code. There is something else I am missing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top