I have an Id column I need to access programmatically, but I don't want it to display in the GridView. I was able to hide it this way, within the Page_Load event of the code-behind:
GridView1.Columns[0].Visible = false;
...but I am not able to retrieve the value from the hidden column. I've got a select button because of setting up my GridView with this: AutoGenerateSelectButton="True"
...but when I try to access the value, it gives me an empty string:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string movieId = string.Empty;
GridViewRow grv = GridView1.SelectedRow;
movieId = grv.Cells[1].Text;
. . .
How can I access that value? I even tried accessing the value at index 0, just in case the "Select" button column is considered to be at index -1, so to speak, as it is not part of the dataset), but that, too, assigns an empty string to movieId! Index 2 does work, but it's not the value I need -- it's the Movie Title.
To be plain, I AM able to hide the "ID" (MovieId) column, between "More" and "Title":
...but I can't access the hidden MovieId column. Being invisible shouldn't mean that it doesn't exist.
My GridView in the aspx file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnDataBound="GridView1_DataBound" OnPreRender="GridView1_PreRender"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
AutoGenerateSelectButton="True">
<Columns>
<asp:BoundField DataField="MovieId" HeaderText="Movie Id" ItemStyle-
Width="0%" SortExpression="MovieId" />
<asp:BoundField DataField="MovieTitle" HeaderText="MovieTitle"
SortExpression="MovieTitle" />
<asp:BoundField DataField="IMDBRating" HeaderText="IMDBRating"
SortExpression="IMDBRating" />
<asp:BoundField DataField="MPAARating" HeaderText="MPAARating"
SortExpression="MPAARating" />
<asp:BoundField DataField="YearReleased" HeaderText="YearReleased"
SortExpression="YearReleased" />
<asp:BoundField DataField="Minutes" HeaderText="Minutes"
SortExpression="Minutes" />
</Columns>
</asp:GridView>
...and in the code-behind (aspx.cs) I have:
protected void GridView1_PreRender(object sender, EventArgs e)
{
GridView1.HeaderRow.Cells[0].Text = "MORE";
GridView1.HeaderRow.Cells[1].Text = "ID";
GridView1.HeaderRow.Cells[2].Text = "TITLE";
GridView1.HeaderRow.Cells[3].Text = "IMDB";
GridView1.HeaderRow.Cells[4].Text = "MPAA";
GridView1.HeaderRow.Cells[5].Text = "YEAR";
GridView1.HeaderRow.Cells[6].Text = "MINUTES";
}
...and again, in the Page_Load event I have:
GridView1.Columns[0].Visible = false;
...which does cause the ID column to "disappear" visually (and seemingly otherwise).
So the "More" (select button) column is at index 0 in the GridView, and I'm expecting ID to be at index 1 (albeit invisible).
I'm trying to grab it in the SelectedIndexChanged event like so:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string movieId = string.Empty;
. . .
GridViewRow grv = GridView1.SelectedRow;
// Index 0 should be the "MORE" (Select button) column; index 1 the
hidden ID (MovieId) column, index 2 the Title (MovieTitle), etc.
movieId = grv.Cells[0].Text; // movieId is "" after this line; the actual
text in that column is "Select"
movieId = grv.Cells[1].Text; // movieId is "" after this line; the column
is invisible
movieId = grv.Cells[2].Text; // movieId is the value in the [Movie] Title
column after this line; visually, this is column index 1, but I'm
expecting it to be actually index 2
. . .

If you have Hidden field like this:
then you have to use
GridView1.SelectedRowproperty followed by the cell index and the value of the control (i.e. Hidden field) like this: