9

I have the GridView below. I am binding to a custom datasource in the code behind. It gets into the "OnRowUpdating" event just fine, but there are no NewValues or OldValues. Any suggestions as to how I can get these values?

<asp:GridView   ID="gv_Personnel" 
                        runat="server" 
                        OnRowDataBound="gv_Personnel_DataBind" 
                        OnRowCancelingEdit="gv_Personnel_CancelEdit" 
                        OnRowEditing="gv_Personnel_EditRow" 
                        OnRowUpdating="gv_Personnel_UpdateRow"
                        AutoGenerateColumns="false" 
                        ShowFooter="true" 
                        DataKeyNames="BudgetLineID"
                        AutoGenerateEditButton="true" 
                        AutoGenerateDeleteButton="true"
                        >
            <Columns>                 
                <asp:BoundField HeaderText="Level of Staff" DataField="LineDescription" />
                <%--<asp:BoundField HeaderText="Hrs/Units requested" DataField="NumberOfUnits" />--%>
                <asp:TemplateField HeaderText="Hrs/Units requested">
                    <ItemTemplate>
                        <%# Eval("NumberOfUnits")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="tb_NumUnits" runat="server" Text='<%# Bind("NumberOfUnits")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Hrs/Units of Applicant Cost Share" DataField="" NullDisplayText="0" />
                <asp:BoundField HeaderText="Hrs/Units of Partner Cost Share" DataField="" NullDisplayText="0" />
                <asp:BoundField FooterStyle-Font-Bold="true" FooterText="TOTAL PERSONNEL SERVICES:" HeaderText="Rate" DataFormatString="{0:C}" DataField="UnitPrice" />
                <asp:TemplateField HeaderText="Amount Requested" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right"  FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
                <asp:TemplateField HeaderText="Applicant Cost Share" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
                <asp:TemplateField HeaderText="Partner Cost Share" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
                <asp:TemplateField HeaderText="Total Projet Cost" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
            </Columns>
        </asp:GridView>
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

5 Answers5

6

Im not sure if this would help..but this is what i found in msdn site

The Keys, OldValues and NewValues collections are automatically populated only when the GridView control is bound to data by using the DataSourceID property.

hallie
  • 2,760
  • 19
  • 27
  • So are you saying that those values can only be used if you are defining and using a datasource on the aspx page? – Abe Miessler Mar 12 '10 at 01:59
  • I'm not sure for I haven't tried but the statement says you need to use the DataSourceID property in binding with the GridView. I might try that and see if you can assign an ID to a datasource and bind it in code behind. If not I'll try the suggestion below =) – hallie Mar 12 '10 at 02:34
4

Regarding on the GridView control's RowUpdating event problem, it is the expected behavior because when we do not associate GridView(or other ASP.NET 2.0 databound control) with DataSource control, it won't automatically query and fill the parameters collection of the updating/deleting/... events. In such cases, we need to manually extract the field values from the Template control.

This is what says a Microsoft employee in here.

In that case you can do it using the ExtractValuesFromCell method to make the NewValues collection yourself.

EDIT:

I found a piece of code in the comments of this blog:

protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{     
       GridView gv = (GridView)sender;
       gv.EditIndex = e.NewEditIndex;
       gv.DataBind();
       ...
}

protected void OnRowUpdating(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);
   }
   // now you can use NewValues collection normally
}

Havent tested it, but seems to solve the problem, let me know if it did.

Travis Collins
  • 3,982
  • 3
  • 31
  • 44
Tony
  • 2,473
  • 3
  • 23
  • 32
  • try e.Keys instead gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, – ONYX Jan 07 '12 at 00:29
  • In my case, the manuel cell values extraction didn't change anything : the NewValues collection remains empty. – Ishikawa Sep 21 '20 at 08:27
2

I was doing databinding in my Page_Load event and when I clicked "Update" the PostBack was done, so was the Page_Load event fired. GridView was filled with the old values again, so in myGrid.RowUpdating event I couldn't retrieve new values. If this helps

tmjam
  • 1,029
  • 2
  • 12
  • 25
1

Bind in the Page PreRender event handler like this:

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    GridBinder()
End Sub

NB: PreRender takes place after the RowUpdating event.

show
  • 69
  • 1
  • 4
0

You need to check the IsPostback in the form load. When the gridview's Update did the postback, it first refreshed with the old values. Use IsPostback to prevent this:

If Not IsPostback Then 

   ' Data bind here

End If

This way, it will not overwrite the "new" gridview values

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • 1
    This does not explain why *old values* are null and hence is not the solution to the issue. It's a common mistake, however, to rebind the grid willy nilly. – user4593252 Jun 01 '16 at 20:27