Ho to Get  Two datakeys From GridView?

Posted on 2008-11-06
Last Modified: 2012-05-05
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

Question by:SaraDob
    LVL 20

    Expert Comment

    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()
    LVL 1

    Author Comment

    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.
    LVL 20

    Accepted Solution

    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

    LVL 1

    Author Closing Comment

    Thank you soo much...You really  cant measure,how much pains you have saved...
    LVL 20

    Expert Comment

    Well, I'm glad it helped! Good luck!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now