Jeff's Blog

SQL Update failure with hidden GridView column

As I'm still fairly new to ASP.NET, I came across what seemed to be a rather simple task that didn't seem possible. I wanted to use a GridView control to display information from an SQL query and allow the user to edit the values in the GridView control. My update SQL statement included a WHERE clause referencing the primary key, an identity field. While it was not necessary to display the value to the user, it is necessary to reference the value in order to update the underlying data. I added the column to my GridView and marked it as hidden. To my surprise, any changes to the data in Edit mode were not saved. I checked and re-checked my update query and everything looked right. I set the column back to visible and voilà, the edit worked properly. After scouring google for a solution and wading through numerous posts that suggested using CSS to hide the data, I finally came across this little blurb in the ASP.NET GridView documentation on the MSDN site:

If the Visible property of a column field is set to false, the column is not displayed in the GridView control and the data for the column does not make a round trip to the client. If you want the data for a column that is not visible to make a round trip, add the field name to the DataKeyNames property.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx 

I checked the properties of GridView and surely, no fields were listed in the DataKeyNames property. I added my primary key and removed the column entirely from the GridView. The data was then updated successfully.

Published Friday, February 02, 2007 11:59 AM by jeff

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jason said:

Thanks buddy! This ended a 30 minute quest.

September 3, 2007 1:52 AM
 

Peter said:

Thanks. You just saved me alot of angst. What a stupid design on Microsoft's part.

February 4, 2008 10:13 AM
 

Kelly said:

I agree with Peter, foolish gridview requirement.  Until I found your explanation of how to fix the issue, I had set my font to 1px and color to match my background, which made the column almost invisible, but it still looked goofy on my page, so thanks for posting this fix. I appreciate it.

June 11, 2008 12:22 PM
 

mallik said:

good post

June 20, 2008 10:11 PM
 

ScottH said:

Wow, this just a major headache for me.  Thanks a bunch.  Cannot believe this has caused so many people trouble.

July 14, 2008 5:05 PM
 

Maria said:

Great post. This cost me an entire afternoon. It's amazing how such obsure tidbits are so difficult to find. Thanks for taking time to share your golden nuggett with us.

August 6, 2008 7:49 PM
 

Chris said:

Thanks - I didn't waste to much time before I found your post

September 8, 2008 9:33 PM
 

Chris said:

After moving past the PK - it was time to deal with the FK

Unlike a primary key or list of composit keys there is nowhere to enter foreign keys. So if you need them for updates here is how you make them invisible. This will make the first column invisible

protected void gvTimeStatus_RowCreated(object sender, GridViewRowEventArgs e)

       {

           // Make the Project FK invisible

           e.Row.Cells[0].Visible = false;

       }

September 8, 2008 10:29 PM
 

Heather said:

Thank you! You just ended a day of frustration!

September 25, 2008 4:04 PM
 

Caleb said:

Like everyone else said, thanks a lot.

October 7, 2008 4:56 PM
 

Stevo said:

Thanks a ton! I was almost going to give up on ASP.NET forever. Disappointing that MSFT has not addressed this in their IDEs.

November 7, 2008 1:12 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Powered by Community Server 2.1