Asked by:
Storing the Key from GridView

Question
-
User-114300688 posted
I am looking to store the value from the selected record in a GridView so that I can Navigate to a new page with this as a session variable.
I have tried many variations of the code (held in the master page) but get a variation of issue. The simplified version of the code below returns and issue that GridView1 is not declared. As you can see from the Sub my gridview is called Grid view1. If I uncomment the Dim statement for Gridview1 and run the screen i get "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object"
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' Dim
Gridview1 As GridView Dim NewText As StringNewText = GridView1.Cells(0)
Session("Table_ID") = NewTextResponse.Redirect(
"../Newpage.aspx") End SubI have also tried variations on what to put in place of the system.EventsArgs and replacing
"NewText = GridView1.Cells(0)"
with
"NewText = e.item.Cells(0)"
as I have seen e.item in other examples but the item does not seem to be a member of System.EventArgs or the other variations I use (i.e GridViewCommandEventArgs)
However DataGridItemEventArgs does allow e.items.Cells(0) but returns the error that it " does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs) "
Any advice would be most greatfully recieved
Sunday, July 2, 2006 11:21 AM
All replies
-
User445179017 posted
Hi,
Set the DataKeyNames of the GridView to the primaryKey of your data, so you can access the value of the selected DataKey in your code this way..
GridView1.SelectedDataKey.Value
ThanksSunday, July 2, 2006 1:05 PM -
User-114300688 posted
Thanks for the advice and such a quick responce.
First of all I set I set
NewText = GridView1.SelectedDataKey.Values
and I get an error that GridView1 is not Declared, so in the same sub I set
Dim
Gridview1 As GridViewHowever it now warns me that
Variable 'GridView1' is used befor it has been assigned a value. A null reference exception could result at runtime.
Which it does.
Could part of the issue be that it is set in the Master page? also another quirk but probably not affect this issue is that this GridView has 2 dataKeys set as it is using two joined tables.
Any further views,?
Thanks in advance.
Sunday, July 2, 2006 2:54 PM -
User445179017 posted
Hi,
When you were using MasterPages, you need to find the GridView control and its not that you declare a variable as GridView.
Have a look at this article, MasterPages and FindControl()
You can have multiple datakeys defined for a GridView.
ThanksSunday, July 2, 2006 3:07 PM -
User-114300688 posted
Got there in the end thanks to your help. I was using the Findcontrol command, but was doing this in the Master document and not the child document. I have now moved the source to the child document and this now works well.
Many thanks again
Simon
Monday, July 3, 2006 6:22 PM -
User2027564069 posted
with DataKeyNames, you can simply use
GridView.SelectedValue [;)]
Tuesday, July 4, 2006 2:41 AM