Set GridView1.SelectedIndex to row Editing without having to click Select first

megnin
megnin used Ask the Experts™
on
I want to use a reference to the SelectedIndex of the row I'm editing in a GridView, without having to click a "Select" button, just the "Edit" button.

I'm trying to send values to a database in code, but it always goes to the selected row, not the row being edited.  If a user forgets to select first then edit, the data will go to the wrong record.  This will happen more often than not, so I don't want to even enable Selection, just Editing.

In a normally databound control in a GridView I know this wouldn't be an issue, but I have the DropDownList in an EditItemTemplate bound to a different field.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
Did you try using the RowEditing event of the grid and using the e.NewEditIndex?

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewediteventargs.aspx
Hi,

I believe this is similar to one of your other questions, You can reference the index of the row being edited using:

GridView1.EditIndex

/Carl.

Author

Commented:
Hi CodeCruiser.  That's a good reference.  I'll have to study that and several links from it at length to get a more complete understanding of what's going on with GridViews and their events.  

Car, I did try using GridView1.EditIndex earlier and it didn't seem to work, but thinking of my recent discovery while in debug, it may have worked, but the value was set back to NULL by the DropDownList or whatever is causing that problem for me right now.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Author

Commented:
Carl,
>>You can't use CType(sender, GridView) in this context as it is the dropdown acting as sender.

>>What You can do is:
>>Dim ApplicantID As Integer = CInt(GridView1.DataKeys(GridView1.EditIndex).Value)

I've got this right now and it's setting ApplicanatID to the correct ID of the person being edited in the GridView.  I'll try your suggestion above.  One line of code is better than taking two to do it...

Dim editedRow As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
Dim ApplicantID As Integer = CType(GridView1.DataKeys(editedRow.RowIndex).Value, Integer)

The code in the ddlVoucherTypeIssued_SelectedIndexChanged event sets the db tables to the correct values. then as soon as the event finishes, the value in VoucherTypeIssued gets set back to NULL by whatever happens right after the DDL changed event.

Author

Commented:
Carl,
I set the command field buttons Save="" Cancel="Save", so when the user things they are clicking the "Save" button, they are just clicking the "Cancel" button because the data has already been written to the DB.

I also put another SqlCommand in the code behind to make the adjustment in the case an existing Voucher is reissued as a different Type, or more accurately if a person who already has a voucher is selected and a different voucher is then selected for them.  Carl, I used your hidden field code to enable me to do that.    Thank you very much for that.  It works like a charm.  :-D
Hey,

I posted in the other question as well but in order to cancel the edit of the gridview, simply add:

GridView1.EditIndex = -1
GridView1.DataBind()

/Carl.

Author

Commented:
Yeah, that was what I needed.  I had actually realized that would do it (found a reference or something) right after I asked.  

I'm going to close the applicable questions.  I've got a new problem.  ;-)

Author

Commented:
Thanks for the help and answers!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial