Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Hide primary Key Column like Visible="false"
 
Share this answer
 
Comments
jmpapa 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.
GridView1.Columns[0].Visible = false;
try this code/.... where colom[colomindex].. here I use 0...
 
Share this answer
 
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...
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.
 
Share this answer
 
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.
Shahriar Iqbal Chowdhury/Galib 17-Feb-13 2:35am    
did u used templatefield? remove the primary key field from template.
TRY WITH THIS..IN ANY OF THE GRIDVIEW EVENT
C#
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
 
Share this answer
 

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