Solved

Gridview Edit Row

Posted on 2016-09-28
10
49 Views
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
0
Comment
Question by:jknj72
  • 8
  • 2
10 Comments
 
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.
0
 

Author Comment

by:jknj72
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...
0
 

Author Comment

by:jknj72
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     ;)
0
 

Author Comment

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

Author Comment

by:jknj72
ID: 41821874
Sorry if I am being so redundant!!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jknj72
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

JK
0
 

Author Comment

by:jknj72
ID: 41821914
DAMMMMMMMIT!!! pic attached
Gridview-problem.png
0
 
LVL 26

Accepted Solution

by:
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
0
 

Author Closing Comment

by:jknj72
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...
0
 

Author Comment

by:jknj72
ID: 41826183
Great work Shaun...THANK YOU!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now