You can't easily access the text from a hidden column in a grid view, but you can access the data in the datasource that it is bound to. Recently I ran into the problem of getting data from a hidden GridView column. I gave up trying to get it from the GridView and focused on the data. Here is how I did it:
Let's assume that we have a gridview called MyGridView with hidden bound column price:
<asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" SortExpression="Price" Visible="false" />MyGridView is Bound to a DataView which was created from a stored procedure:
MyGridView .DataSource = null;
DataTable
table = GetProducts(); // indirectly call stored proc to get dataDataView dv = new DataView(table); // make a data view
dv.Sort = "Price ASC"; // sort it by price
MyGridView.DataSource = dv;
MyGridView.DataBind();
1st) We save the DataView in the Cache. Why the Cache? We can't use a Session to save a DataView because Sessions wont serialize DataViews, it can only serialize DataSets. You may note that a problem that arises with the Cache (unlike the Session State) is we don't want everyone sharing the same Data View from different Web Browsers. So we trick the Cache into acting like a Session by tagging the user name onto the Cache key (You may think of a better way of giving the Cache key a unique name such as a HashCode):
string sUserName = System.Web.HttpContext.Current.User.Identity.Name; Cache["PriceView"+sUserName] = dv; 2nd) To trap a row selection, you can define an event handler in your gridview markup to trap the selection of a row button or link using the OnRowCommand attribute. Just define the name of your event handler method and you can use it in your code behind:
slbrookPosted Oct 3, 2008, 10:04 AM
DataKeys can be used if the field is that type - Id field. This allows the id field to be hidden and still used.
Raj KumarPosted Jan 9, 2008, 6:38 AM
Hey thanks mike you solved my problem. i was searching for same problem.