Hi,
I have a gridview that is being databound from a DataTable from code behind:
PosterGV.DataSource = dt
PosterGV.DataBind()
This works fine.
I need to get the values from the row in which a linkbutton is clicked, and this works too:
Protected Sub Andra_post(ByVal sender As Object, ByVal e As System.EventArgs)
Dim valdLB As LinkButton = TryCast(sender, LinkButton) Dim gvr As GridViewRow = TryCast(valdLB.NamingContainer, GridViewRow)
Dim i As Integer = gvr.RowIndex - 4 'Minus four KarADDL.SelectedValue = CInt(gvr.Cells(0).Text)
BeloTB.Text = gvr.Cells(1).Text
Et cetera... The problem is that two of the values must be hidden. Thus I try to use datakeynames, but can't get it to work. I have set the dkn in the gridview tag:
DataKeyNames="inkl, sats"
I have also set them in Page.Load:
PosterGV.DataKeyNames = New String() {"inkl", "sats"}
...as well as in the datatable:
dt.Columns.Add("inkl")
dt.Columns.Add(
"sats")
I know they are in the datatable because they show up in the gridview.
Now, when I try to retrieve the values in the sub shown above, it won't work (it worked when they were visible normal values).
This is my code for now:
myDDL.SelectedIndex = CInt(PosterGV.SelectedDataKey(0).Value.ToString())
I cant understand what's wrong, but I'm sure it has to do with the change to dkn. The error I get says the following:
System.NullReferenceException was unhandled by user code
Message="Objektreferensen har inte angetts till en instans av ett objekt." (Meaning: The object reference has not been assigned an instance of an object)
Source="App_Web_l_icdvjb"
StackTrace:
vid eko_reg.Andra_post(Object sender, EventArgs e) i C:\Documents and Settings\ ... .aspx.vb:rad 790
vid System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
vid System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
vid System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
vid System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
vid System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
vid System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Please help me out! (I've tried setting the dkn of the gridview after the datasource has been defined, but to no avail.)
Pettrer, Stockholm
Coding is a nine-to-five job: Nine PM to Five AM.