Accessing Data From a selected row in a Hidden GridView Column

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 data

DataView 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:

<asp:GridView ID="MyGridView" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

AllowSorting="True" OnSorting="GridUnassignedMorningAssignments_Sorting" AutoGenerateColumns="False" OnSelectedIndexChanged="GridUnassignedMorningAssignments_SelectedIndexChanged" OnRowCommand="OnGotoPressed" >

3rd) Now we want to get the row selected using the event parameter e

public void OnGotoPressed( Object src, GridViewCommandEventArgs e ) {

// get the row index stored in the CommandArgument property
int index = Convert.ToInt32 ( e.CommandArgument );

// get the actual selected row using the row index
GridViewRow selectedRow = ( ( GridView ) e.CommandSource ).Rows [ index ];

// Note:  You can retrieve the text of non-hidden columns like this:

string nonHiddenData = selectedRow.Cells[4].Text

4th)  Pull the data from the DataView directly corresponding to the DataItemIndex. Since the DataView already syncs to the GridView, we can be sure that the index corresponds to the data. 

int dataIndex = selectedRow.DataItemIndex; // index into dataview

//  retrieve the user name again to add to the Cache Key

string sUserName = System.Web.HttpContext.Current.User.Identity.Name;

// retrieve the DataView of interest from the Cache

DataView dv = Cache["PriceView"+sUserName] as DataView;

// get the price which is in the hidden row
float price(float)(dv[dataIndex]["Price"]));

And That's It!

 

Ebook Download
View all
Learn
View all