Can data be kept in a dynamically bound GridView's invisible fields?


I have a query expression which I am binding to a GridView in Page_Load. The data I want to capture in the SelectedIndexChaned event is in a BoundField defined thus:

<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" Visible="False" />

If I set Visible="True", I have no trouble getting this data. Is there a way to hide the ID field and still get the data?


Depends on how you are trying to get the data. If this is an ID field that is unique for each row in the datasource, use DataKeyNames = "ID" in the GridView declaration. Then, in the code behind, whenever you need the ID, you can use the following line:

string ID = GridView1.Rows[GridRowIndex].DataKeys[0].Value.ToString();

You could also convert one of your BoundFields to a TemplateField, and place a HiddenField in it to store the ID. Like so:

<asp:TemplateField> <ItemTemplate> <asp:Label ID="someOtherDataLabel" runat="server" /> <asp:HiddenField ID="IDHiddenField" runat=server /> </ItemTemplate> </asp:TemplateField>

Then you could use FindControl() in the RowDataBound event of the GridView to store the ID value.


  • how to access templatefield from code behind
  • Retrieve values from GridView by changing a value in a DropDownList
  • Grid view sorting for particular column
  • Is it possible to have an editable DetailsView for entity objects with subclasses?
  • Primefaces Dialog Framework — dialogReturn event from menuitem
  • Controls in ItemTemplate can't be called in Code Behind
  • How to change display text in item template based on eval in gridview?
  • Should a C# struct have only read-only properties
  • How to edit a Word document that opens in read-mode in VBA
  • DependencyObject.AssociatedObject is always null
  • Accessing another variable in the same class with a click event
  • How do you keep a running instance for Google App Engine
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • Why can't UI components be accessed from a backgroundworker?
  • How to getText() from the input field of an angularjs Application
  • Disable Kendo Autocomplete
  • Suppressing passwd when calling sqlplus from shell script
  • Tamper-proof configuration files in .NET?
  • OOP Javascript - Is “get property” method necessary?
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • JQuery Internet Explorer and ajaxstop
  • Android full screen on only one activity?
  • Retrieving value from sql ExecuteScalar()
  • Illegal mix of collations for operation for date/time comparison
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • Cant find why the layout is getting smaller
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?