We help IT Professionals succeed at work.

asp.net gridview datasource databind not working 2nd time

ToolTimeGang asked
My page has a gridview that edits data without using DetailsView or FormView.
The issue occurs on postback.  

I am using this code to rebind my gridview after the user changes the row(where mdt is a datatable).  This allows me to save the "bulk edit" updates to a datatable which is then saved in ViewState.
gvMoveDetails.DataSourceID = String.Empty
gvMoveDetails.DataSource = New DataView(mdt)

Here is my problem.
The first time this code runs on a postback, it works fine.
But the 2nd time it does not.
The data for the fields that are not visible is not there anymore.
Before that code is run, I know for sure all the data is in mdt.  But as soon as databind is executed, the fields that are not visible to the browser can no longer be accessed in the code behind.

Note that I am not setting the visibility in the code behind.  instead, I am using css:

Please help me! I have spent sooo much time on this and am sooo very frustrated!

Watch Question


I finally figured it out.  And guess what?  I have been bit by this before!  
The piece I was missing is to include “Readonly=True” in the html for the bound field that is not visible.

<asp:BoundField DataField="ToLocID" ReadOnly="true"></asp:BoundField>

When the code behind was trying to access the data, it kept trying to get the EditItemTemplate Instead of the ItemTemplate data.

Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.