Getting the row index of a command in gridview control

In my gridview I have a textbox and a save icon which allows a user to enter a date and click save which saves the date to the database for that record pointed to in the row. The problem I have is that I don’t know how to get the row to process the save in the codebehind. Normally I would set the command-argument equal to the product ID and then in the codebehind handle the event where the commandname matches the commandname for the save icon. I have the problem then though of not knowing which row caused the event.

If I use a buttonfield, I know it sets the commandargument to the row index, but the problem I have then is having no way of getting the product_id.
I need the row index in order to use gridview.rows[index].findcontrol() to be able to get the date out of the text box that the user is typing the date into.

Can anyone solve this conundrum and suggest the best way to overcome this issue?



10/16/2007 11:36:44 AM 91979 articles. 4 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 0

If ProductID is a primary key, you can use the DataKeyNames property of the GridView.

Set DataKeyNames="ProductID".

 Then, you can get the ProductID of the nth row with the expression:



10/16/2007 12:22:11 PM

Hello, use the gridView _RowUpdating event:

protected void gridView1_RowUpdating(object ender, GridViewUpdateEventArgs e)


    //    Code to save the values. 

    // You can modify them here again if you have any business logic

    // e.NewValues["DateValue"]

    // e.OldValues["DateValue"] 




Also in the aspx use <%# Bind("DateValue") %> instead of EVal

and for your save button have the : CommandName="Update"

Hope this would help and solve your problem.

Mehdi Entezary
Please click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
10/16/2007 12:33:51 PM