Gridview Edit Row

Posted on 2016-09-28
Last Modified: 2016-10-03
Is it possible to get the e.NewValues of a gridview row in Edit mode if you did NOT set up any type of Edit template? I have Boundfields in the row instead of TemplateFields and I have a Commandfield for my Edit button. When I go into Edit mode, after I set the EditIndex to NewEditIndex in the RowEditing event, I just want to know if it is possible to access the "e.NewValues" in RowUpdating event. If not then its ok but I dont want to spend anymore time trying to get these values if its not in the cards. I have seen some javascript "work arounds" but that isnt the way I want to do it. If I have to create an EditItemTemplate dynamically then thats fine but I want to know before I waste anymore time on this....Thanks
Question by:jknj72
  • 8
  • 2
LVL 26

Expert Comment

by:Shaun Kline
ID: 41821539
You can get the values in the row that is being edited, but the template fields make it easier to get to the controls. To get to the values without a template field, you need to have a reference to the row, then find the desired cell, and finally the control in that cell: Gridview.Rows(Gridview.EditIndex).Cells(<your column #>).Controls(0).

As you can see, that is not pretty. When you use template fields, you can give your controls names and then use the FindControl method to search as container (your edit row) for a control with a specific name: GridView.Rows(Gridview.EditIndex.FindControl(<your control's name>).

Beyond that, by using template fields you can add validation to your controls that do not automatically happen when you use bound fields.

Author Comment

ID: 41821831
Thanks for the response Shaun. I tried to add the Template field to the columns being created dynamically but wasnt able to figure it out and Im afraid if I spend too much more time and Im not able to get it done Im kinda scr3wed. Do you happen to have any code where I can create the Template fields dynamically? If not, Im going to try and use the Gridview.Rows(Gridview.EditIndex).Cells(<your column #>).Controls(0). and see what happens and if that doesnt work I am going to use the popup that I use to create the "Item" and see if I can create an Edit mode in that popup form instead of editing the rows in the gridview.
Please feel free to recommend any type of solution you think may be the best way to go for me.
Just a quick scenario just in case you are not aware,
I have a gridview user control that has 3 modes to create 3 different items.(Work, travel and image). In this form I can create 3 different types of Items and they will end up being the data in the gridview. I use the control to dynamically to set the columns, datasource and navigation. The control however, doesnt have the Edit and Delete capabilities and the columns are primarily only Bound Fields. I was able to add  command fields to the gridview at the end to add the Edit and Delete button. My problem is that I cannot get to the cell values and that is where I am stuck.
Like I said, any type of recommendation on the best (and quickest) solution would really help me out and I really appreciate your help...

Author Comment

ID: 41821843
Also, I have not created any type of controls in the Bound Fields for when it goes into Edit mode(label or textbox). I guess the control automatically adds textboxes/labels and fills them with the gridview cells values. Do they even have a name to them? Im not sure what or how I would reference them? But luke I said Im gonna try and get to them with your code. Ive been trying to get to these values for a couple of days now. And my boss is freaking out because I told him I could get to them and he was right because I cant. If I can though, with your help or anyone else for that matter, I will send you a steak dinner!!!!  Hahaha...And Im not kidding!!!  Thanks again

JK     ;)
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Author Comment

ID: 41821845
oh yeah one last thing....where do I put this line of code? RowEditing, RowUpdating, RowDataBound???

Author Comment

ID: 41821874
Sorry if I am being so redundant!!

Author Comment

ID: 41821902
Man I am so sorry to keep posting to this question but I guess I should have mentioned this. When I was trying to extract values from the row in Edit mode , I have been getting the same error EVERY SINGLE TIME.... Since the beginning I have not been able to assign any type of index to the Row in order to evaluate what is in any cell I have been getting the same exact error with the index out of range. Youll notice that I can set the EditIndex to the NewIndex in RowDataBound, which is 1 because it is the second row in the grid. But when I set the grid.Rows(grid.EditIndex or e.NewIndex) I get the same error in every event too...Sorry for writing so many posts but everytime I send a message I think of something else...Thanks for your patience


Author Comment

ID: 41821914
DAMMMMMMMIT!!! pic attached
LVL 26

Accepted Solution

Shaun Kline earned 500 total points
ID: 41823415
Similar to how you did the page change event, if you wish to use the GridView's built in edit mode, you would need to perform a data bind after setting the EditIndex value. This allows the GridView to use all of the Edit styles, and, when using bound field, change each cell into a control that represents the type of data in that cell: Text box for most data, checkbox for bit fields. If you were using an edit template, the gridview would display it. Once the GridView is in edit mode, you can use the code I provided earlier.

The reason you were getting the error is because the gridview wasn't rebound after setting the EditIndex, and thus there were no controls in the cells. You could have accessed the data in the cells without the databind by instead using Gridview.Rows(Gridview.EditIndex).Cells(<your column #>).Text.

If you do not want to use the GridView for editing your data, you may want to consider using the Selected Index Changing event. It works somewhat similar as the RowEditing, in that when the event is fired, the GridViewSelectEventArgs parameter passed to the method has the property NewSelectedIndex. You set that to the GridView's SelectedIndex property and do the databind, and the Gridview uses any of the Select styles (such as changing the background color of the selected row.) From their, you can use the selected row index to access the data in the cells using: Gridview.Rows(Gridview.SelectedIndex).Cells(<your column #>).Text

Author Closing Comment

ID: 41826182
You were right and and I cant tell you how grateful I am because this was a big problem for me. As for the SeletedIndexChanged(ing) event, I have never been able to get that event to fire. I am not sure what the problem is but I wanted to try and use that but for some reason I havent been able to get that to fire. Maybe I can repost a question for that though and again, thank you so much for your help and for putting in the time for me...

Author Comment

ID: 41826183
Great work Shaun...THANK YOU!!!

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question