title: Get old Gridview Row values in the RowUpdating event
body: <p>How do I get the old values in a gridview row in the row_updating event? </p>
  
  <p>I'm using a sqldatasource control to populate the grid and want to write the update code in the RowUpdating event.</p>
  
  <p>There are 10 fields displayed in the grid.</p>
  
  <p>I can get the value of the first field using </p>
  
  <pre><code>string old_Category = e.OldValues[0].ToString();.
  </code></pre>
  
  <p>But it doesn't work for index items greater than 0.  This doesn't work</p>
  
  <pre><code>string old_Category = e.OldValues[1].ToString();
  </code></pre>
  
  <p>it returns an error message</p>
  
  <pre><code>Index was out of range. Must be non-negative and less than the size of the collection.
  Parameter name: index
  </code></pre>
  
  <p>even though there are 10 columns in the grid.</p>
  
  <p>========================================
  Here's the gridview markup</p>
  
  <pre><code>            &lt;asp:GridView 
                  ID="GridView1" 
                  runat="server" 
                  AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource1" 
                  BorderWidth="0px"
                  BorderStyle="None" 
                  Width="100%" 
                  CellPadding="2" 
                  PageSize="25" 
                  OnRowDeleting="ProjectListGridView_RowDeleting" 
                  onrowdatabound="GridView1_RowDataBound" 
                  onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing" &gt;
                  &lt;Columns&gt;
                      &lt;asp:CommandField ShowEditButton="True" /&gt;
                      &lt;asp:TemplateField HeaderText="WeekEnding" SortExpression="WeekEnding"&gt;
  
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="lblWeekEnding" runat="server" Text='&lt;%# Bind("WeekEnding") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Project" SortExpression="Project"&gt;
                         &lt;EditItemTemplate&gt;
  
                              &lt;asp:DropDownList 
                                  ID="DropDownList1" 
                                  runat="server" 
                                  DataSourceID="sdsDdlProjectsEdit" 
                                  DataTextField="ProjectName" 
                                  DataValueField="ProjectID" 
                                  AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
  
                                  &gt;
                              &lt;/asp:DropDownList&gt;
  
                              &lt;asp:SqlDataSource 
                                  ID="sdsDdlProjectsEdit" 
                                  runat="server" 
                                  ConnectionString="&lt;%$ ConnectionStrings:ttuser %&gt;" 
                                  SelectCommand="SELECT ProjectID,ProjectName FROM dbo.aspnet_starterkits_Projects"&gt;
                              &lt;/asp:SqlDataSource&gt;
  
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("Project") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
  
                      &lt;asp:TemplateField HeaderText="Category" SortExpression="Category"&gt;
  
                          &lt;EditItemTemplate&gt;
  
  
                              &lt;asp:DropDownList 
                                  ID="DropDownList2" 
                                  runat="server" 
                                  DataSourceID="sdsDDL2" 
                                  DataTextField="CategoryName" 
                                  DataValueField="CategoryID"&gt;
                              &lt;/asp:DropDownList&gt;
  
                              &lt;asp:SqlDataSource 
                              runat="server" 
                              ID="sdsDDL2"
                              ConnectionString="&lt;%$ ConnectionStrings:ttuser %&gt;"
                              SelectCommand="SELECT [CategoryID], [CategoryName],[ProjectID] FROM dbo.aspnet_starterkits_ProjectCategories WHERE ([ProjectID]=@ProjectID)"&gt;
  
                               &lt;SelectParameters&gt;
                                   &lt;asp:Parameter Name="ProjectID" /&gt;
                               &lt;/SelectParameters&gt;
  
                              &lt;/asp:SqlDataSource&gt;
  
  
  
                          &lt;/EditItemTemplate&gt;
  
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Bind("Category") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
  
                      &lt;/asp:TemplateField&gt;
  
                      &lt;asp:TemplateField HeaderText="Sun" SortExpression="Sun"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtSunEdit" runat="server" Text='&lt;%# Eval("Sun") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
  
                              &lt;asp:Label ID="lblSun" runat="server" Text='&lt;%# Bind("Sun") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
  
                      &lt;asp:TemplateField HeaderText="Mon" SortExpression="Mon"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtMonEdit" runat="server" Text='&lt;%# Eval("Mon") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
  
                              &lt;asp:Label ID="Label4" runat="server" Text='&lt;%# Bind("Mon") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Tue" SortExpression="Tue"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtTueEdit" runat="server" Text='&lt;%# Eval("Tue") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label5" runat="server" Text='&lt;%# Bind("Tue") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Wed" SortExpression="Wed"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtWedEdit" runat="server" Text='&lt;%# Eval("Wed") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label6" runat="server" Text='&lt;%# Bind("Wed") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Thu" SortExpression="Thu"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtThuEdit" runat="server" Text='&lt;%# Eval("Thu") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label7" runat="server" Text='&lt;%# Bind("Thu") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Fri" SortExpression="Fri"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtFriEdit" runat="server" Text='&lt;%# Eval("Fri") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label8" runat="server" Text='&lt;%# Bind("Fri") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Sat" SortExpression="Sat"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtSatEdit" runat="server" Text='&lt;%# Eval("Sat") %&gt;' Columns="2"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="Label9" runat="server" Text='&lt;%# Bind("Sat") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
              &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>and here is the sqldatasource</p>
  
  <pre><code>                    &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                          ConnectionString="&lt;%$ ConnectionStrings:ttuser %&gt;" 
  SelectCommand="SELECT
     @WeekEnding   WeekEnding
    ,Project
    ,ProjectID
    ,Category
    ,CategoryID
    ,isnull([1], 0)  Sun
    ,isnull([2], 0)  Mon
    ,isnull([3], 0)  Tue
    ,isnull([4], 0)  Wed
    ,isnull([5], 0)  Thu
    ,isnull([6], 0)  Fri
    ,isnull([7], 0)  Sat
   from 
  (select P.ProjectName Project,P.ProjectID, CAT.CategoryName Category,CAT.CategoryID, Datepart(dw, TE.TimeEntryDate) DOW, TE.TimeEntryDuration Hours
   FROM 
  dbo.aspnet_starterkits_TimeEntry TE inner join
  dbo.aspnet_starterkits_ProjectCategories CAT on
  TE.CategoryID=CAT.CategoryID inner join
  dbo.aspnet_starterkits_Projects P on
  CAT.ProjectID=P.ProjectID
  Where (TE.TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding) AND
  TE.TimeEntryUserID=(SELECT UserId FROM dbo.aspnet_Users WHERE UserName=@UserName)) Source
    pivot (max(Hours)for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt" OldValuesParameterFormatString="original_{0}" 
  
  
  
  
                          &lt;SelectParameters&gt;
                              &lt;asp:ControlParameter ControlID="WeekEnding2" Name="WeekEnding" 
                                  PropertyName="Text" /&gt;
                              &lt;asp:ControlParameter ControlID="UserList" Name="UserName" 
                                  PropertyName="SelectedValue" /&gt;
                          &lt;/SelectParameters&gt;
  
  
  
                      &lt;/asp:SqlDataSource&gt;
  </code></pre>