|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to get GridView row from DataKey?Is there an easy way to get a GridView row given a DataKey? Right now, I'm
looping through each row of the GridView and checking its DataKey, but it seems that there must be a better way. Thanks! Greg Greg,
I have found that I cannot often rely on the DataKey. Instead when I do have a button in a GridView I set the CommandArgument for the button to the identifier for the data in that row. Hopefully that will be a primary key which you can use for the actions you plan to take. I set the CommandArgument (and CommandName to provide some context) in the RowCreated event which is called for each row. You can access the data for that row with e.Item.DataItem which will not be null when the GridView is first bound. But on PostBacks you will find null references since only the bound values in the related controls are persisted through ViewState. Brennan Stehling http://brennan.offwhite.net/blog/ Greg wrote: Show quoteHide quote > Is there an easy way to get a GridView row given a DataKey? Right now, I'm > looping through each row of the GridView and checking its DataKey, but it > seems that there must be a better way. > > Thanks! > Greg Brennan,
Thanks, that's an interesting idea. I've had problems with the DataKey before myself. Another page I worked on recently had an empty DataKeys collection after posting back. I ended up having to rebind the grid after the postback in order to get the DataKey, but I think your solution would have been better. In this case though, I don't think it will work for me. What I'm working on now is a wizard, where one of the steps has a gridview where each row has a text column filled from the database, plus a couple of check boxes. The check boxes are being checked/unchecked from an object in the viewstate when the user gets to that step, so if the user returns to the step, the changes are still there, but they aren't saved to the database until the end of the wizard. (There may be a better way to do this, but I don't want to make this post even longer with too many details.) So basically, I know the DataKey of the rows where I need to check boxes, and was hoping there's an easy way to get the row without looping through each row to see if the DataKey matches. But I like your suggestion. That answers the question I had earlier, and will ultimately be more useful than saving 10 lines of code looping through rows in a GridView. Thank you, Greg Show quoteHide quote "Brennan Stehling" wrote: > Greg, > > I have found that I cannot often rely on the DataKey. Instead when I > do have a button in a GridView I set the CommandArgument for the button > to the identifier for the data in that row. Hopefully that will be a > primary key which you can use for the actions you plan to take. > > I set the CommandArgument (and CommandName to provide some context) in > the RowCreated event which is called for each row. You can access the > data for that row with e.Item.DataItem which will not be null when the > GridView is first bound. But on PostBacks you will find null > references since only the bound values in the related controls are > persisted through ViewState. > > Brennan Stehling > http://brennan.offwhite.net/blog/ > > Greg wrote: > > Is there an easy way to get a GridView row given a DataKey? Right now, I'm > > looping through each row of the GridView and checking its DataKey, but it > > seems that there must be a better way. > > > > Thanks! > > Greg > > hay, im having the same problem
im trying to retrive the primary key of the record in the grid view. i've set the datakeynames="id" and the OnRowCommand to refer to my vb procedure. Dim index As Integer = Convert.ToInt32(e.CommandArgument) Dim id As Integer = CType(GridView1.DataKeys(index).Values("Id"), Integer) i just seem to be getting a null value. im trying to populate the record data into freetextbox. thankz Ko$tA,
That appears to be working code, but I have had trouble making the DataKey work reliably. Instead of placing the index in the CommandArgument, use the value of the primary key. That is what I use. In ASP.NET 1.1 it was common to place a column in the DataGrid for the primary key, but the Visible property on the column is set to false. But when I try that in ASP.NET 2.0 it does not bind the column. That is why I am now using the CommandArgument on a button. What may fix this situation is using a typed DataSet. I often bind a collection of business objects instead of typed DataSets as is highly suggested by MSDN documentation. Using a typed DataSet should identify the primary key which is used for the DataKey. Brennan Stehling http://brennan.offwhite.net/blog/ Ko$tA wrote: Show quoteHide quote > hay, im having the same problem > > im trying to retrive the primary key of the record in the grid view. i've set the datakeynames="id" and the OnRowCommand to refer to my vb procedure. > > > Dim index As Integer = Convert.ToInt32(e.CommandArgument) > > Dim id As Integer = CType(GridView1.DataKeys(index).Values("Id"), Integer) > > i just seem to be getting a null value. > > im trying to populate the record data into freetextbox. > > thankz
ERROR: The state information is invalid for this page and might be corrupted
Properties do not reliably persist using CompositeControl themes Images Drop Down List issue Item has already been added. Key in dictionary can't remove listitem from ddl in loop SiteMap resolve question Parameter as web user control DetailsView without DataSource Problem using themes in web user controls |
|||||||||||||||||||||||