Click to See Complete Forum and Search --> : Loop through records and update


andywhat
03-04-2009, 02:59 PM
Hi, first time posting here. I have a problem with an application that I am trying to finish. I have a gridview with items, and a button that says "process" now when this button is pressed I need to for all the records shown in the gridview update a value. It's a basic inventory system, so when an item is taken out of the inventory (processed) then the value in inventory needs to be updated.

Thanks in advance! :)

lmf232s
03-04-2009, 04:38 PM
the gridview has a rows collection that you can use to loop through each row. As well there is a property of the gridview that you can set that will hold the 'KeyId' of the row. I believe its DataKey or DataKeys and you can have multiple values in the keys, you just need to make sure you reference the right key if you do so.

This link will show you the basics of looping the collection of rows:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrowcollection.aspx

andywhat
03-06-2009, 03:49 PM
thanks for the reply that should work but I'm stuck in one part of the code:

If e.Row.RowType = DataControlRowType.Footer Then

Works only if its in the RowCreated sub, but I am calling this from a button, so when the user presses the "process" button then it should starting going through the records. Thanks in advance.

lmf232s
03-06-2009, 04:42 PM
While e.row.rowtype does not work in your instance there is a way once you have got your rows to determine the type of row. Off the to of my head I do not know the exact syntax.

The problem is that row 0 is going to be the header row and the last row is the footer row. I can't remember what happens or what causes it to happen but something about trying to access controls while the row is pointing to these 2 will cause an error to be thrown. Its been a while so this may not be the exact reason but again I remember there being an issue when trying to reference things from the header or footer.

see if this helps:
http://forums.asp.net/p/1042272/1454130.aspx