VB gridview value from a hidden column

I am uing a gridview to display a list of orders, and when the user clicks on the selected order header the detail comes up. I am using the Id from the order header to get the order detail.

This works fine and the code behind is:-

 Private Sub GridView1_SelectedIndexChanged
            StockPotrnId = GridView1.SelectedRow.Cells(2).Text
        Catch ex As Exception

        End Try
        Session("CurrentDate") = Calendar1.SelectedDate
        Session("StockPotrnId") = StockPotrnId
        Response.Redirect("frmstockorderdetail.aspx?StockPotrnId=" + StockPotrnId.ToString())
    End Sub

Now the grid is getting full and I would like to hide the cell containing the Id to give me more room, but if I hide the cell this doesn't work. Is there any way round this?
Who is Participating?
Obadiah ChristopherConnect With a Mentor Commented:
Alternatively you could try the GridView Datakey approach

Instead of using Visible=false, use style="display:none" which will make the column render but stay hidden so you can still access it.
grwallaceAuthor Commented:
The Gridview Datakey approach does work, but I can't get the style="Display:none" approach.
The code for the column is:-

 <asp:BoundField DataField="PoTrnId" HeaderText="PoTrnId" InsertVisible="False"
            ReadOnly="True" SortExpression="PoTrnId"  itemstyle-width="3em"   >
<ItemStyle Width="3em"  ></ItemStyle>

Exactly where should I insert this to get it to work? The reason I ask is that while the primary key is exactly what I am looking for in this example, on the next page it is not, and if I can get the Style option to work that would do the trick in the next example
Obadiah ChristopherConnect With a Mentor Commented:
It has to be done in GridView Row DataBound event.

if(e.Row.RowType == DataControlRowType.DataRow

Open in new window

CodeCruiserConnect With a Mentor Commented:
I think its the ControlStyle property for the boundfield

if that does not work, then try converting it into template column.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.