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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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     ;)
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

752 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