GridView RowUpdating function need OldValues, please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anniebai
    New Member
    • Aug 2007
    • 51

    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.Cou nt, e.NewValues.Cou nt -----------> 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
    Code:
     <asp:GridView ID="gvStudentsAndProjects" runat="server" AutoGenerateColumns="False"  DataKeyNames="StudentID,ProjectName" AllowSorting="True"  
    Cellpadding="4" CssClass="gridview" GridLines="None" ShowFooter="True" EnableViewState=false  
    OnRowCommand="gvStudentsAndProjects_RowCommand" 
    OnRowCancelingEdit="gvStudentsAndProjects_RowCancelingEdit"  
    OnRowEditing ="gvStudentsAndProjects_RowEditing" 
    OnRowUpdating="gvStudentsAndProjects_RowUpdating">
    There're seven columns, here's a block of code for one column.

    Code:
    <asp:TemplateField HeaderText="ProjectName" SortExpression="ProjectName">
                    <EditItemTemplate>
                        <asp:TextBox ID="tbProjectName" runat="server" Text='<%# Bind("ProjectName") %>' Width="100%" Rows="2" TextMode="MultiLine"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvProjectName" runat ="server" ControlToValidate ="tbProjectName"
                        Display="Dynamic" Text="*Please input a project name" ErrorMessage="ProjectName Cannot be empty" 
                        ValidationGroup="EditValidationControls"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbProjectName" runat="server" Text='<%# Bind("ProjectName") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="tbNewProjectName" runat="server" Rows="2" TextMode="MultiLine" Width="100%"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvNewProjectName" runat ="server" ControlToValidate ="tbNewProjectName"
                        Display="Dynamic" Text="*Please input a project name" ErrorMessage="ProjectName Cannot be empty" 
                        ValidationGroup="InsertValidationControls"></asp:RequiredFieldValidator> 
                    </FooterTemplate>
                    <FooterStyle Wrap =True />
    </asp:TemplateField>
    Last edited by Frinavale; Apr 27 '10, 01:05 PM. Reason: Changed html tags into code tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • anniebai
      New Member
      • Aug 2007
      • 51

      #3
      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.

      Code:
      protected void Page_Load(object sender, EventArgs e)
          {
              databind();
          }
      
          protected void databind()
          {
              gvStudentsAndProjects.DataSource = adapter.GetProjectsByStudentID(studentid);
              gvStudentsAndProjects.DataBind();
          }
      
      protected void gvStudentsAndProjects_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {
              GridView gv = (GridView)sender;
              for (int i = 0; i < gv.Columns.Count; i++)
              {
                  DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
                  gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
              }
              e.Cancel = true;
              databind();
          }

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by anniebai
        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.

        Code:
        protected void Page_Load(object sender, EventArgs e)
            {
                databind();
            }
        
            protected void databind()
            {
                gvStudentsAndProjects.DataSource = adapter.GetProjectsByStudentID(studentid);
                gvStudentsAndProjects.DataBind();
            }
        
        protected void gvStudentsAndProjects_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                GridView gv = (GridView)sender;
                for (int i = 0; i < gv.Columns.Count; i++)
                {
                    DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
                    gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
                }
                e.Cancel = true;
                databind();
            }
        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?

        Comment

        • rahma
          New Member
          • Apr 2010
          • 1

          #5
          suggestion

          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)
          {
          BindUsersGridVi ew();
          }
          and now every thing is well.
          hoping this will help you.

          Comment

          Working...