473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

GridView RowUpdating function need OldValues, please help

Please help me with writing a RowUpdating function in C#, I don't know how to grab the current field's value and also get the old value for one of keys (which is ProjectName for editing) of the selected row. I've tried:
e.Keys.Count, e.OldValues.Count, e.NewValues.Count -----------> all give zero
Some said if TemplateField is used, e.Keys and Oldvalues, NewValues are all empty. Then what suppose to be used in such case?
Thanks for any inputs.

Here is the code for GridView
Expand|Select|Wrap|Line Numbers
  1.  <asp:GridView ID="gvStudentsAndProjects" runat="server" AutoGenerateColumns="False"  DataKeyNames="StudentID,ProjectName" AllowSorting="True"  
  2. Cellpadding="4" CssClass="gridview" GridLines="None" ShowFooter="True" EnableViewState=false  
  3. OnRowCommand="gvStudentsAndProjects_RowCommand" 
  4. OnRowCancelingEdit="gvStudentsAndProjects_RowCancelingEdit"  
  5. OnRowEditing ="gvStudentsAndProjects_RowEditing" 
  6. OnRowUpdating="gvStudentsAndProjects_RowUpdating">
  7.  
There're seven columns, here's a block of code for one column.

Expand|Select|Wrap|Line Numbers
  1. <asp:TemplateField HeaderText="ProjectName" SortExpression="ProjectName">
  2.                 <EditItemTemplate>
  3.                     <asp:TextBox ID="tbProjectName" runat="server" Text='<%# Bind("ProjectName") %>' Width="100%" Rows="2" TextMode="MultiLine"></asp:TextBox>
  4.                     <asp:RequiredFieldValidator ID="rfvProjectName" runat ="server" ControlToValidate ="tbProjectName"
  5.                     Display="Dynamic" Text="*Please input a project name" ErrorMessage="ProjectName Cannot be empty" 
  6.                     ValidationGroup="EditValidationControls"></asp:RequiredFieldValidator>
  7.                 </EditItemTemplate>
  8.                 <ItemTemplate>
  9.                     <asp:Label ID="lbProjectName" runat="server" Text='<%# Bind("ProjectName") %>'></asp:Label>
  10.                 </ItemTemplate>
  11.                 <FooterTemplate>
  12.                     <asp:TextBox ID="tbNewProjectName" runat="server" Rows="2" TextMode="MultiLine" Width="100%"></asp:TextBox>
  13.                     <asp:RequiredFieldValidator ID="rfvNewProjectName" runat ="server" ControlToValidate ="tbNewProjectName"
  14.                     Display="Dynamic" Text="*Please input a project name" ErrorMessage="ProjectName Cannot be empty" 
  15.                     ValidationGroup="InsertValidationControls"></asp:RequiredFieldValidator> 
  16.                 </FooterTemplate>
  17.                 <FooterStyle Wrap =True />
  18. </asp:TemplateField>
  19.  
Oct 31 '07 #1
4 12465
Frinavale
9,735 Expert Mod 8TB
The most common problem for losing new data entered while updating a GridView or DataGrid is that you are resetting the DataSource for the GridView or DataGrid in the Page_Load method.

If you reset the DataSource at this stage, your values for updating will be lost.

Please post the code for your Page_Load method and the that handles your Update (your C# code) so we can have a better look at what's going on.

Thanks

-Frinny
Oct 31 '07 #2
Thanks for reply.
Here is the Page_Load function. I actually don't know how to write the RowUpdating function, I just found it from the internet, and so of course it's not working. Please give some references and/or details of how to. Thanks a lot in advance.

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         databind();
  4.     }
  5.  
  6.     protected void databind()
  7.     {
  8.         gvStudentsAndProjects.DataSource = adapter.GetProjectsByStudentID(studentid);
  9.         gvStudentsAndProjects.DataBind();
  10.     }
  11.  
  12. protected void gvStudentsAndProjects_RowUpdating(object sender, GridViewUpdateEventArgs e)
  13.     {
  14.         GridView gv = (GridView)sender;
  15.         for (int i = 0; i < gv.Columns.Count; i++)
  16.         {
  17.             DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
  18.             gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
  19.         }
  20.         e.Cancel = true;
  21.         databind();
  22.     }
  23.  
Oct 31 '07 #3
Frinavale
9,735 Expert Mod 8TB
Thanks for reply.
Here is the Page_Load function. I actually don't know how to write the RowUpdating function, I just found it from the internet, and so of course it's not working. Please give some references and/or details of how to. Thanks a lot in advance.

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         databind();
  4.     }
  5.  
  6.     protected void databind()
  7.     {
  8.         gvStudentsAndProjects.DataSource = adapter.GetProjectsByStudentID(studentid);
  9.         gvStudentsAndProjects.DataBind();
  10.     }
  11.  
  12. protected void gvStudentsAndProjects_RowUpdating(object sender, GridViewUpdateEventArgs e)
  13.     {
  14.         GridView gv = (GridView)sender;
  15.         for (int i = 0; i < gv.Columns.Count; i++)
  16.         {
  17.             DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
  18.             gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
  19.         }
  20.         e.Cancel = true;
  21.         databind();
  22.     }
  23.  
Ok what's happening is that you are binding the data source for the GridView every time a postback occurs in the Page_Load method.

This wipes out the data provided by the user during editing (editing happens after the page load).

So what you need to do is store the data source (maybe in Session) and only bind rebind the data when you absolutely need to (Like during the Is Not PostBack)

Do you understand?
Nov 1 '07 #4
rahma
1
hello
my problem was that my gridview editable controls heep their old values. new values are lost. this is due to the grid view binding in the page_load method after every post back.
the idea wa to don't allow binding the grid view after a post back.
the solution is to make this test :
if (!IsPostBack)
{
BindUsersGridView();
}
and now every thing is well.
hoping this will help you.
Apr 27 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Olbert | last post by:
I ran into a situation this morning where the RowUpdating event of a GridView kept insisting there were no entries in either the OldValues or NewValues dictionaries. The GridView is bound to a...
0
by: Michael | last post by:
Hi I'm having a problem getting the newvalues or oldvalues from the RowUpdating event. I have the following code for the event(See note): Protected Sub grdPOs_RowUpdating(ByVal sender As Object,...
0
by: Michael | last post by:
Hi I'm having a problem getting the newvalues or oldvalues from the RowUpdating event. I have the following code for the event(See note): Protected Sub grdPOs_RowUpdating(ByVal sender As Object,...
3
by: slemen | last post by:
The controls (textboxes) in the gridview row being updated have the old, pre user updated values in the RowUpdating event. Does anyone have an idea why? Thank you, Scott
1
by: s.bussing | last post by:
Hi, I have been struggling with the GridView the whole day, but can not get this solved. The rowupdating event in my Gridview doesn't give me the new values only the old ones. In my GridView I...
9
by: Mel | last post by:
I have 10 columns total. 3 of them are invisible. The rest are read- only BoundFields, 3 of which are editable fields using TemplateFields. Upon editing, I want to validate what the user enters...
1
by: Steve Kershaw | last post by:
Hi, I'm using the RowUpdating() event of an updatable GridView. I need to see the values of the columns in the updated row. There has got to be a way to do this! Any suggestions? Thanks...
0
by: lightgram | last post by:
Hello I am having problems extracting the OldValues collection from the RowUpdating event in my GridView. Every time I access this I am getting the values from the NewValues collection in...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.