Ho to Get Two datakeys From GridView?

Hi Coding Experts

I have a Grid view, where i have set 2 datakeys( Travel Id and location).
I'm hiding TravelID column, but displaying location in the gridview.On selection of the location,i want to display Location in a textbox, but still have the travelid in a variable.Travelid is a GUID.
I'm not able to get The location in the text box.

here is the piece of code i'm using to get the values:

Also, If i have large number of locations(rows), will the Onselectedchange event handler be the right one to use?

Help Appreciated, Thanks in advance...

Protected Sub GrdLocation_SelectedindexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GrdLocation.SelectedIndexChanged 
Dim TravelId As Guid 
 TravelId = Me.GrdLocation.DataKeys(GrdLocation.SelectedIndex).Value("TravelLocID") .....This gives me an error saing out of index 
TravelId = Me.GrdLocation.DataKeys(e.CommandArgument).Value() ......This give me error saying command argument is not a part of Selected indexchanged
'TxtLocation.Text = Me.GrdLocation.DataKeys(GrdLocation.SelectedIndex).Value("Destination").ToString. .....does not get the value says index out of range
Dim row As GridViewRow = GrdLocation.SelectedRow 
TxtLocation.Text = row.Cells(1).Text.ToString ...No Errors but not able to get the value in text box
End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

HI SaraDob,

You should be using GrdLocationSelectedIndexChanging to handle your event. In that way, you get an argument e of type GridViewSelectEventArgs. It will give you the new selected index ( e.NewSelectedIndex).
From there, you can access the respective datakeys by using: Me.GrdLocation .DataKeys(e.NewSelectedIndex).Value()
SaraDobAuthor Commented:
Hi Master,
I'm writing this logging from home, as i waited for this solution from morning and left work.
I will work this out from office morrow, as will def let you know about it.
No problem!
Just a clarification: In the example I gave you, I forgot to change the .Value() for the index or name of the dataKey. So it would be like this

Me.GrdLocation .DataKeys(e.NewSelectedIndex)(indexOftheValueIntheDataKeysCollecion")


Me.GrdLocation .DataKeys(e.NewSelectedIndex)("nameOftheValue")

So, in your case, is:

Dim TravelId As Guid = Ctype(Me.GrdLocation.DataKeys(e.NewSelectedIndex)("TravelLocID"),Guid)

Open in new window


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
SaraDobAuthor Commented:
Thank you soo much...You really  cant measure,how much pains you have saved...
Well, I'm glad it helped! Good luck!
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
Web Applications

From novice to tech pro — start learning today.