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?

 

Thanks

0
elDeeJay
10/16/2007 11:36:44 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

2 Replies
613 Views

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:

GridView1.DataKeys(n)

Jos

0
Jos
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"] 

    //e.NewValues["ID"] 

}

 

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.
0
Mehdi6002
10/16/2007 12:33:51 PM
Reply: