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.
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 thankssteve-----------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.ItemIndexEnd Sub JBelthoffDodge, Duck, Dip, Dive & DodgeIf a man can dodge a wrench, he can dodge a ball! Asp Hoting Provider |
 |
|
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! |
 |
|
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.or2) 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. |
 |
|
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 sodg.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 FialaVery Practical Software, IncNow with Blogging...http://www.vpsw.com/blogbabyMicrosoft MVP |
 |
|
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 wantsteve-----------Oh, so they have internet on computers now! |
 |
|
|
|
|
|
|