Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 This must be easy

Author  Topic 

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-05-23 : 07:02:02
I'm sure this is easy but I can't seem to work it out

I have a datagrid, in there is a primary key field (hidden). The user selects a row and then later presses a button. When they press the button I want to know the value of the primary key of the selected record.

I know this is something standard but I just can't seem to find how to do it!!!

Any pointers would be greatly appreciated.

Many thanks

steve


-----------

Oh, so they have internet on computers now!

JBelthoff
Posting Yak Master

173 Posts

Posted - 2006-05-23 : 07:39:34
I think it is something like this... No?
Sub dgProducts_Edit(sender As Object, e As DataGridCommandEventArgs)
whatever = e.Item.ItemIndex
End Sub




JBelthoff
D
odge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Asp Hoting Provider
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-05-23 : 08:25:33
Many thanks I'll take a look at that

-----------

Oh, so they have internet on computers now!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-05-23 : 08:51:34
Two ways --

1) pass the PK in the commmandArgs of the button that is pressed.

or

2) if you have your DataKeyField set to the column that is the PK, then you can use:

YourDataGrid.DataKeys(e.Item.ItemIndex)

to return the PK. be sure to cast to the proper data type.
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-05-23 : 08:54:36
Before you bind the grid, you need to tell it the primary key field like so

dg.DataKeyField = "SomeID"
dg.DataBind()

Then you can retrieve the associated key from the DataKeys collection using the selected Item's ItemIndex property like so (assuming an integer key)...

Dim ID as Integer = CType(dg.DataKeys(e.Item.ItemIndex), Int32)



Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-05-23 : 11:15:08
quote:
dg.DataKeyField = "SomeID"
dg.DataBind()


Just realised I had these the wrong way round. D'oh!!

Many thanks to all - it now works as I want

steve

-----------

Oh, so they have internet on computers now!
Go to Top of Page
   

- Advertisement -