Click here to Skip to main content
12,400,183 members (49,076 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
Is it possible to get the primary key of selected gridview row without the display of the primarykey column? How to do this? Thanks.
Posted 15-Feb-13 22:39pm
jmpapa445
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hide primary Key Column like Visible="false"
  Permalink  
Comments
Member 9807240 16-Feb-13 20:46pm
   
I tried to do that but there's an error saying "Index was out of range. Must be non-negative and less than the size of the collection."

I am using a databound. Is it still possible to hide the column? Thank you in advance.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

GridView1.Columns[0].Visible = false;
try this code/.... where colom[colomindex].. here I use 0...
  Permalink  
Comments
jmpapa 17-Feb-13 1:50am
   
I have tried to do this yesterday but it keeps on having an error saying "Index was out of range. Must be non-negative and less than the size of the collection." I think it's because I'm using a databound. Is there other way to do this?
Avik Ghosh22 17-Feb-13 1:58am
   
than use java script to hide column..

http://aspdotnetcodebook.blogspot.in/2008/03/how-to-hide-gridview-column-using.html
http://www.yiiframework.com/forum/index.php/topic/19707-showhide-gridview-columns-in-javascript/
http://dotnetspeaks.net/post/Hiding-Columns-in-GridView-without-losing-data.aspx
http://www.dotnettwitter.com/2011/03/adding-removing-gridview-columns-in.html
http://www.jqwidgets.com/show-hide-gridview-columns/
Avik Ghosh22 17-Feb-13 1:59am
   
other wise u can provide me ur code for check...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi,
Hiding column is not good solution to your problem, You can easily solve your issue with templatefield and DataKey. assign your primary key column name into DataKey. Checkout the example below,

http://msdn.microsoft.com/en-us/library/bb288031.aspx[^]

Later you can get the primary key of the selected row by using,
DataKey currentKey = myGridView.SelectedDataKey;
Hope this will help.
  Permalink  
Comments
jmpapa 17-Feb-13 1:58am
   
I also tried to do this, but then, I'm having an error saying that the variable I used for datakey is not present in the current content. So, I included it in my select query which makes my primary key field visible.
   
did u used templatefield? remove the primary key field from template.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

TRY WITH THIS..IN ANY OF THE GRIDVIEW EVENT
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100