We help IT Professionals succeed at work.

Unable to update GridView data on RowUpdating

L00M asked
Last Modified: 2013-11-07
I've added a gridview to my project, and it's displaying and sorting properly. However, when I click on 'Update' on a row (after clicking'edit' and making a change), I'm not able to find or see the oldvalues or newvalues in GridView1_RowUpdating. I can see that e.RowIndex is set properly, but that's about it. Also, I'm using a cache object, as seen in the code below, and I've never used that before. (I did a datagrid several years ago...). Is the cache needed?
I know this problem is due to late binding, but I like having complete control over my object. I'm not a big fan of drag and drop and then magic server pixies do the rest.

Code In Front:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="workshopgroups_main.aspx.vb" Inherits="administration_workshopgroups_main" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table bgcolor="<% =System.Configuration.ConfigurationManager.AppSettings.Get("Admin_BGColor")%>" width="100%">
            <td align="center" colspan="3">
                <strong><span class="adminlink"><a href="default.aspx">Calendar Management</a></span>
                    &gt; Workshop Groups</strong></td>
            <td colspan="3">
            <td colspan="3">
                <asp:Label ID="lblWorkshopGroups" runat="server"></asp:Label></td>
            <td colspan="3">
                <asp:GridView id="gvWorkShopGroups" runat="server"
                    AllowSorting="True" Width="100%" DataKeyNames="id">
                        <asp:CommandField ShowEditButton="True">
                            <ItemStyle Width="20px" Wrap="False" />
                        <asp:CommandField ShowDeleteButton="True">
                            <ItemStyle Width="20px" Wrap="False" />
                        <asp:BoundField DataField="Name" HeaderText="Name" >
                            <ItemStyle HorizontalAlign="Left" Wrap="False" />
                            <HeaderStyle HorizontalAlign="Left" />
                        <asp:BoundField DataField="Ordering" HeaderText="Ordering" >
                            <ItemStyle HorizontalAlign="Right" Wrap="False" />
                            <HeaderStyle HorizontalAlign="Right" />
                        <asp:BoundField DataField="id" HeaderText="id" />
                    <HeaderStyle BackColor="Navy" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="#EEEEEE" />

Code Behind:
Imports System.Configuration.ConfigurationManager
Imports System.Data.SqlClient
Imports System.Data
Imports System.Diagnostics

Partial Class administration_workshopgroups_main
    Inherits System.Web.UI.Page
    Dim wsg As workshopgroup
    Dim dt As DataTable
    Dim dv As DataView

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            wsg = New workshopgroup
            dt = New DataTable
            dv = New DataView
            dt = wsg.LoadAll() '<- Returns a datatable with multiple lines and the following fields: id, name, ordering
            dv = New DataView(dt)

            ' Cache the view so we can sort on it later
            ' Is this really needed? I never had to use Cache in the past...
            Cache("dv") = dv
            Cache("dt") = dt
        End If
    End Sub

    Private Sub BindGrid()
        'Bind the DataTable to the GridView
        gvWorkShopGroups.DataSource = dt
    End Sub

    Protected Sub gvWorkShopGroups_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvWorkShopGroups.RowEditing
        Dim dv As New DataView
        dv = Cache("dv")

        gvWorkShopGroups.DataSource = dv
        gvWorkShopGroups.EditIndex = e.NewEditIndex
    End Sub

    Protected Sub gvWorkShopGroups_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvWorkShopGroups.RowUpdating
        Dim row As GridViewRow = gvWorkShopGroups.Rows(e.RowIndex)
        If row IsNot Nothing Then
            ' Below produces nothing
            ' This does show the correct row that I am updating ...
        End If
    End Sub

    Protected Sub gvWorkShopGroups_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvWorkShopGroups.Sorting
        Dim dv As New DataView
        dv = Cache("dv")

        dv.Sort = e.SortExpression.ToString() & " DESC"
        gvWorkShopGroups.DataSource = dv
    End Sub
End Class
Watch Question

Where's the Update query in RowUpdating?

Cache can be used if you're sorting the grid. Otherwise, it's not necessary.


Ah, then that's why I'm using Cache. I probably wont need sorting, but found an example online and thought I'd add it.

As for the update query, it's not specified yet, because I can't find the data that has been changed so I can run an update query. I've found sever 'For Each' examples online to iterate through the 'NewValues' and 'OldValues' collections, but it skips right over the loops, so the collections are empty.


I just added the following code to my RowUpdating event:

    Protected Sub gvWorkShopGroups_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvWorkShopGroups.RowUpdating
        Dim row As GridViewRow = gvWorkShopGroups.Rows(e.RowIndex)
        Dim i As Integer
        For i = 0 To row.Cells.Count - 1
            Debug.WriteLine("Cells(" & i & ") = " & row.Cells(i).Text)
    End Sub

It fires off just fine, but in my debutg window, I see:

Cells(0) =
Cells(1) =
Cells(2) =
Cells(3) =
Cells(4) =

Where are the values? New or old...
I suspect this has something to do with the DataBinding, but am confused on how to implement it.

Unlock this solution and get a sample of our free trial.
(No credit card required)


Well slap my behind and call me Polly!
It seems like that's going to work digitalZo....

Lemme do some more testing, but it looks like you get the gold star.


Looks like that will work. Can you clear up one more detail? You're getting the poinks no matter what.

I'm adding the ID column to the datagrid as well, but there is no need for the end user to actually see the id, I just need it for updating. However, when I set that column to 'visible=false', I'm unable to retrieve the value. The CType works, and when I debug, I can see it automatically sets the new textbox 'ToolTip' to ID, so that's a good sign. But the 'text' property is blank.

Have you set the DataKey in GridView? If not, then set the DataKey to ID [in the Source code] and this code should only retrieve the value, not edit or update it [unless you specify the TextBox thing]:

<asp:GridView DataKeyNames="ID" . . . >

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

Dim ID = GridView1.DataKeys(e.RowIndex).Value


My apologies for not responding sooner. Busy busy.

Ok, I can successfully return the new values using the code:

        id = gvWorkShopGroups.DataKeys(e.RowIndex).Value
        tb = CType(Me.gvWorkShopGroups.Rows(e.RowIndex).Cells(2).Controls(0), TextBox)
        NewName = tb.Text
        tb = CType(Me.gvWorkShopGroups.Rows(e.RowIndex).Cells(3).Controls(0), TextBox)
        NewOrdering = tb.Text

However, per your suggection, I'd like to use the following code as it is cleaner:

        id = gvWorkShopGroups.DataKeys(e.RowIndex).Value
        Dim row As GridViewRow = Me.gvWorkShopGroups.Rows(e.RowIndex)
        Dim NewName As TextBox = row.FindControl("name")
        Dim NewOrdering As TextBox = row.FindControl("ordering")

However, nothing is returned into those variables. When debugging, I can see 'row' is populated, and has cell values available, but I'm guessing the cells aren't named and can only be referred to by index number. Since the grid will be dynamically populated, how to I give the columns proper names? (Assuming I've diagnosed the problem correctly.)


Got it. After converting all fields to Template fieldsd, I was able to access everything properly.

Thanks for all your help!
Glad it worked.

The cleaner code you suggested didn't actually work for me. The alternative one worked, which is why I posted that one.



Thanks Zo!


Thanks Zo. I forgot I had this question open. Sorry so long in responding.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.