Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to acces a datakey column into my grid, in the event of row editting, MY code is

VB
Private Sub grdOdGovori_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdOdGovori.RowEditing
     

     
      Apoen = CDec(grdOdGovori.Rows(e.NewEditIndex).Cells(1).Text)
     
      VidTestiraniPari = grdOdGovori.SelectedDataKey("Sifratestirani").ToString
      BindGridRezultati()

  End Sub


And the error is: Object reference not set to an instance of an object. How can I acces the Datakey??
Posted

1 solution

in your gird aspx page, set the DataKeyNames property as your key column name, for example if it is ID then
C#
asp:GridView DataKeyNames ="ID" 

then in your event method you can get the id value as below
C#
grdOdGovori.DataKeys(e.RowIndex).Value

ref:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames(v=vs.110).aspx[^]
if you have multiple keys as below
asp:GridView DataKeyNames ="ID,ID2" 

then you can get each key as below
grdOdGovori.DataKeys(e.RowIndex).Values("ID1").ToString() and grdOdGovori.DataKeys(e.RowIndex).Values("ID2").ToString()
 
Share this answer
 
v3
Comments
Voley 28-May-14 8:53am    
I have the same name as in the gridVie... I have two datakeys in my grid? Can that be the problem? Myy Key looks like DataKeyNames="Id1,Id2" ???
DamithSL 28-May-14 9:08am    
check my updated answer
Voley 28-May-14 9:21am    
I tryied that before, and the error was this "'RowIndex' is not a member of System.Web.UI.WebControls.GridViewEdit.EventArgs" --I am using that int row deleting and it works but here NO! :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900