I have a GridView with a DataSource
(SQL Database). I want to hide a column, but still be able to access the value when I select the record. Can someone show me how to do this?
This is the column I want to hide and still want to access its value:
<asp:BoundField DataField="Outlook_ID" HeaderText="OutlookID" />
I tried everything to hide the column (property Visible="false"
), but I can't access its value.
Leave visible columns before filling the
GridView
. Fill theGridView
and then hide the columns.You can make the column
hidden
on the server side and for some reason this is different to doing it theaspx
code. It can still be referenced as if it was visible. Just add this code to yourOnDataBound
event.You can do it code behind.
Set visible= false
for columns after data binding . After that you can again do visibility "true" in row_selection function from grid view .It will work!!If you do have a
TemplateField
inside the columns of yourGridView
and you have, say, a control named blah bound to it. Then place theoutlook_id
as aHiddenField
there like this:Now, grab the row in the event you want the
outlook_id
and then access the control.For
RowDataBound
access it like:Do get back, if you have trouble accessing the clicked row. And don't forget to mention the event at which you would like to access that.
You can use DataKeys for retrieving the value of such fields, because (as you said) when you set a normal
BoundField
as visible false you cannot get their value.In the
.aspx
file set theGridView
propertyNow, in an event handler you can access the value of this key like so:
This will give you the id at the specified rowindex of the grid.
I used a method similar to user496892: