How to read items during databinding which don't appear in GridView?
In ASP.NET, using a GridView control. Assume my SQLDataSource has an ID, Name and Address. I am only showing Name and Address in the grid, but I want to read the ID every time a row is databound to the grid. How woul开发者_如何学God I do this?
Thanks
The best way in these scenarios is to set the DatakeyName
of your ID
column in the GridView.
You may use it later on where you need DataKeyNames="ID"
You can bind the ID to a hiddenfield or to any control and make that control's visible property false.
What you need to do is get access to the underlying DataViewRow Object through the Gridview's RowDataBound Event. Once you have the DataViewRow you have access to the DataItem object this is the actual object your GridView is bound to. MSDN has an example here.
Cheers
精彩评论