title: How can I UPDATE specific GridView columns based on a CheckBox in one column
body: <p>My GridView has an "Active" CheckBox, to indicate whether the record is active or not.  It's just a flag, a bit value in the database.</p>
  
  <p>I need to UPDATE the "ActivatedBy" and "DeactivatedBy" fields when a user checks or unchecks the CheckBox (in edit mode).  </p>
  
  <p>I'm currently updating the "UpdatedBy" field every time a row is updated, filling it with @UserName. </p>
  
  <p>But, I don't know how to programatically update either the ActivatedBy or DeactivatedBy fields only when the CheckBox is checked or unchecked respectively.  This is just to record who changes the checkbox (@UserName is the working parameter).</p>
  
  <p>Please assume I need the details spelled out for me.  I do.  ;-)  I'm just learning C# and GridViews are my bane.  Thank you!</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Admin" MaintainScrollPositionOnPostback="true" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Admin&lt;/title&gt;
      &lt;style&gt;
          body {
              font-family:Arial;
              font-size:12px;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="programid" DataSourceID="ITAAdminSqlDataSource" ForeColor="#333333" GridLines="None"&gt;
              &lt;AlternatingRowStyle BackColor="White" /&gt;
              &lt;Columns&gt;
                  &lt;asp:CommandField ShowEditButton="True" ShowSelectButton="True" /&gt;
                  &lt;asp:BoundField DataField="programid" HeaderText="programid" InsertVisible="False" ReadOnly="True" SortExpression="programid" Visible="False" /&gt;
                  &lt;asp:BoundField DataField="OccGroup" HeaderText="Occ Group" SortExpression="OccGroup" /&gt;
                  &lt;asp:BoundField DataField="Provider" HeaderText="Provider" SortExpression="Provider" /&gt;
                  &lt;asp:BoundField DataField="Program" HeaderText="Program" SortExpression="Program" /&gt;
                  &lt;asp:BoundField DataField="OnetCode" HeaderText="Onet Code" SortExpression="OnetCode" /&gt;
                  &lt;asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /&gt;
                  &lt;asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /&gt;
                  &lt;asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" &gt;
                  &lt;ItemStyle Wrap="False" /&gt;
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="TuitionCosts" DataFormatString="{0:c}" HeaderText="Tuition" HtmlEncode="False" SortExpression="TuitionCosts" /&gt;
                  &lt;asp:BoundField DataField="OtherCosts" DataFormatString="{0:c}" HeaderText="Other Costs" HtmlEncode="False" SortExpression="OtherCosts" /&gt;
                  &lt;asp:BoundField DataField="SpecialConditions" HeaderText="Special Conditions" SortExpression="SpecialConditions" /&gt;
                  &lt;asp:BoundField DataField="Credential" HeaderText="Credential" SortExpression="Credential" /&gt;
                  &lt;asp:CheckBoxField DataField="Active" HeaderText="Active" SortExpression="Active" /&gt;
                  &lt;asp:BoundField DataField="DateEntered" DataFormatString="{0:d}" HeaderText="Date Entered" HtmlEncode="False" SortExpression="DateEntered" /&gt;
                  &lt;asp:BoundField DataField="EnteredBy" HeaderText="Entered By" SortExpression="EnteredBy" /&gt;
                  &lt;asp:BoundField DataField="DateUpdated" DataFormatString="{0:d}" HeaderText="Date Updated" HtmlEncode="False" SortExpression="DateUpdated" /&gt;
                  &lt;asp:BoundField DataField="UpdatedBy" HeaderText="Updated By" SortExpression="UpdatedBy" /&gt;
                  &lt;asp:BoundField DataField="DateActivated" DataFormatString="{0:d}" HeaderText="Date Activated" HtmlEncode="False" SortExpression="DateActivated" /&gt;
                  &lt;asp:BoundField DataField="ActivatedBy" HeaderText="Activated By" SortExpression="ActivatedBy" /&gt;
                  &lt;asp:BoundField DataField="DateDeactivated" DataFormatString="{0:d}" HeaderText="Date Deactivated" HtmlEncode="False" SortExpression="DateDeactivated" /&gt;
                  &lt;asp:BoundField DataField="DeactivatedBy" HeaderText="Deactivated By" SortExpression="DeactivatedBy" /&gt;
              &lt;/Columns&gt;
              &lt;EditRowStyle BackColor="Tomato" /&gt;
              &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
              &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
              &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
              &lt;RowStyle BackColor="#EFF3FB" /&gt;
              &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
              &lt;SortedAscendingCellStyle BackColor="#F5F7FB" /&gt;
              &lt;SortedAscendingHeaderStyle BackColor="#6D95E1" /&gt;
              &lt;SortedDescendingCellStyle BackColor="#E9EBEF" /&gt;
              &lt;SortedDescendingHeaderStyle BackColor="#4870BE" /&gt;
          &lt;/asp:GridView&gt;
          &lt;asp:SqlDataSource ID="ITAAdminSqlDataSource" runat="server" ConnectionString="&lt;%$ ConnectionStrings:ITAAdminConnectionString %&gt;" 
              DeleteCommand="DELETE FROM [programs] WHERE [programid] = @programid" 
              InsertCommand="INSERT INTO [programs] ([OccGroup], [Provider], [Program], [OnetCode], [Address], [City], [Telephone], [TuitionCosts], [OtherCosts], [SpecialConditions], [Credential], [Active], [DateEntered], [EnteredBy], [DateUpdated], [UpdatedBy], [DateActivated], [ActivatedBy], [DateDeactivated], [DeactivatedBy]) VALUES (@OccGroup, @Provider, @Program, @OnetCode, @Address, @City, @Telephone, @TuitionCosts, @OtherCosts, @SpecialConditions, @Credential, @Active, @DateEntered, @EnteredBy, @DateUpdated, @UpdatedBy, @DateActivated, @ActivatedBy, @DateDeactivated, @DeactivatedBy)" 
              SelectCommand="SELECT [programid], [OccGroup], [Provider], [Program], [OnetCode], [Address], [City], [Telephone], [TuitionCosts], [OtherCosts], [SpecialConditions], [Credential], [Active], [DateEntered], [EnteredBy], [DateUpdated], [UpdatedBy], [DateActivated], [ActivatedBy], [DateDeactivated], [DeactivatedBy] FROM [programs] ORDER BY [Provider], [Program]" 
              UpdateCommand="UPDATE [programs] SET [OccGroup] = @OccGroup, [Provider] = @Provider, [Program] = @Program, [OnetCode] = @OnetCode, [Address] = @Address, [City] = @City, [Telephone] = @Telephone, [TuitionCosts] = @TuitionCosts, [OtherCosts] = @OtherCosts, [SpecialConditions] = @SpecialConditions, [Credential] = @Credential, [Active] = @Active, [DateEntered] = @DateEntered, [EnteredBy] = @EnteredBy, [DateUpdated] = GETDATE(), [UpdatedBy] = @UserName, [DateActivated] = @DateActivated, [ActivatedBy] = @ActivatedBy, [DateDeactivated] = @DateDeactivated, [DeactivatedBy] = @DeactivatedBy WHERE [programid] = @programid"&gt;
              &lt;DeleteParameters&gt;
                  &lt;asp:Parameter Name="programid" Type="Int32" /&gt;
              &lt;/DeleteParameters&gt;
              &lt;InsertParameters&gt;
                  &lt;asp:Parameter Name="OccGroup" Type="String" /&gt;
                  &lt;asp:Parameter Name="Provider" Type="String" /&gt;
                  &lt;asp:Parameter Name="Program" Type="String" /&gt;
                  &lt;asp:Parameter Name="OnetCode" Type="Int32" /&gt;
                  &lt;asp:Parameter Name="Address" Type="String" /&gt;
                  &lt;asp:Parameter Name="City" Type="String" /&gt;
                  &lt;asp:Parameter Name="Telephone" Type="String" /&gt;
                  &lt;asp:Parameter Name="TuitionCosts" Type="Decimal" /&gt;
                  &lt;asp:Parameter Name="OtherCosts" Type="Decimal" /&gt;
                  &lt;asp:Parameter Name="SpecialConditions" Type="String" /&gt;
                  &lt;asp:Parameter Name="Credential" Type="String" /&gt;
                  &lt;asp:Parameter Name="Active" Type="Boolean" /&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateEntered" /&gt;
                  &lt;asp:Parameter Name="EnteredBy" Type="String" /&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateUpdated" /&gt;
                  &lt;asp:Parameter Name="UpdatedBy" Type="String" /&gt;
                  &lt;asp:Parameter Name="username" Type="String" DefaultValue="Anonymous" /&gt; &lt;%-- username parameter filled in Page_Init. use @username instead of UpdatedBy, ActivatedBy, etc. --%&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateActivated" /&gt;
                  &lt;asp:Parameter Name="ActivatedBy" Type="String" /&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateDeactivated" /&gt;
                  &lt;asp:Parameter Name="DeactivatedBy" Type="String" /&gt;
              &lt;/InsertParameters&gt;
              &lt;UpdateParameters&gt;
                  &lt;asp:Parameter Name="OccGroup" Type="String" /&gt;
                  &lt;asp:Parameter Name="Provider" Type="String" /&gt;
                  &lt;asp:Parameter Name="Program" Type="String" /&gt;
                  &lt;asp:Parameter Name="OnetCode" Type="Int32" /&gt;
                  &lt;asp:Parameter Name="Address" Type="String" /&gt;
                  &lt;asp:Parameter Name="City" Type="String" /&gt;
                  &lt;asp:Parameter Name="Telephone" Type="String" /&gt;
                  &lt;asp:Parameter Name="TuitionCosts" Type="Decimal" /&gt;
                  &lt;asp:Parameter Name="OtherCosts" Type="Decimal" /&gt;
                  &lt;asp:Parameter Name="SpecialConditions" Type="String" /&gt;
                  &lt;asp:Parameter Name="Credential" Type="String" /&gt;
                  &lt;asp:Parameter Name="Active" Type="Boolean" /&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateEntered" /&gt;
                  &lt;asp:Parameter Name="EnteredBy" Type="String" /&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateUpdated" /&gt;
                  &lt;asp:Parameter Name="UpdatedBy" Type="String" /&gt;
                  &lt;asp:Parameter Name="username" Type="String" DefaultValue="Anonymous" /&gt; &lt;%-- username parameter filled in Page_Init. use @username instead of UpdatedBy, ActivatedBy, etc. --%&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateActivated" /&gt;
                  &lt;asp:Parameter Name="ActivatedBy" Type="String" /&gt;
                  &lt;asp:Parameter DbType="Date" Name="DateDeactivated" /&gt;
                  &lt;asp:Parameter Name="DeactivatedBy" Type="String" /&gt;
                  &lt;asp:Parameter Name="programid" Type="Int32" /&gt;
              &lt;/UpdateParameters&gt;
          &lt;/asp:SqlDataSource&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  using System;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Admin : System.Web.UI.Page
  {
      protected string ClearDomain(string sItem) //Format username without domain
      {
          int sLoc = (sItem.IndexOf("\\") + 1);
          string sOutPut;
          sOutPut = sItem.Substring(sLoc);
          return sOutPut;
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          //string usr;
          //usr = ClearDomain(User.Identity.Name.ToString());
      }
  
      protected void Page_Init(object sender, EventArgs e)
      {
          ITAAdminSqlDataSource.InsertParameters["UserName"].DefaultValue = ClearDomain(User.Identity.Name.ToString());
          ITAAdminSqlDataSource.UpdateParameters["UserName"].DefaultValue = ClearDomain(User.Identity.Name.ToString());
      }
  }
  </code></pre>