title: How to code a button click event in a GridView that returns a value from its row in the GridView
body: <p>Apologies for the slightly convoluted title.</p>
  
  <p>Basically, I have a project where I have a database connected to it. I'm displaying some of the contents of the database using a GridView, which is all up and running, but the way I'm designing the page I need to be able to click a button in each row that in essence exports the value of one of the cells in the row to a subroutine. </p>
  
  <p>I've tried googling it quite a bit and trying related things I found here, but nothing I could find would function like I would like it to.</p>
  
  <p>The relevant code is below.</p>
  
  <p>Markup: </p>
  
  <pre><code>&lt;asp:GridView ID="Staffgv" runat="server" AutoGenerateColumns="false" OnRowCommand="Staffgv_RowCommand" AllowPaging="true" PageSize="20" OnPageIndexChanging="Staffgv_PageIndexChanging" BackColor="#f9f9f9" CssClass="gvStyle" &gt;
  
     &lt;Columns&gt;
  
           &lt;asp:TemplateField HeaderText="Start" InsertVisible="False" SortExpression="DateTimeStart"&gt;
                  &lt;HeaderStyle Width="70px"   CssClass="hdrGvStart"/&gt;
                  &lt;ItemTemplate&gt;
  
                           &lt;asp:Label ID="lblDateTimeStart" runat="server" Text='&lt;%# Bind("DateTimeStart", "{0:t}") %&gt;'&gt;&lt;/asp:Label&gt;
  
                  &lt;/ItemTemplate&gt;      
  
            &lt;/asp:TemplateField&gt;
  
            &lt;asp:TemplateField HeaderText="Finish" SortExpression="DateTimeEnd"&gt;
                   &lt;HeaderStyle Width="70px"   CssClass="hdrGvFinish"/&gt;
                   &lt;ItemTemplate&gt;
  
                           &lt;asp:Label ID="lblDateTimeEnd" runat="server" Text='&lt;%# Bind("DateTimeEnd", "{0:t}") %&gt;'&gt;&lt;/asp:Label&gt;
  
                   &lt;/ItemTemplate&gt;
  
            &lt;/asp:TemplateField&gt;
  
            &lt;asp:TemplateField HeaderText="Forename" SortExpression="Forename"&gt;
                   &lt;HeaderStyle Width="140px"  CssClass="hdrGvForename"/&gt;
                   &lt;ItemTemplate&gt;
  
                           &lt;asp:Label ID="lblForename" runat="server" Text='&lt;%# Bind("Forename") %&gt;'&gt;&lt;/asp:Label&gt;
  
                   &lt;/ItemTemplate&gt;               
  
            &lt;/asp:TemplateField&gt;  
  
            &lt;asp:TemplateField HeaderText="Surname" SortExpression="Surname"&gt;
                    &lt;HeaderStyle Width="140px"   CssClass="hdrGvSurname"/&gt;
                    &lt;ItemTemplate&gt;
  
                           &lt;asp:Label ID="lblSurname" runat="server" Text='&lt;%# Bind("Surname") %&gt;'&gt;&lt;/asp:Label&gt;
  
                    &lt;/ItemTemplate&gt;               
  
            &lt;/asp:TemplateField&gt;  
  
            &lt;asp:TemplateField&gt;
                    &lt;HeaderStyle CssClass="gvHeaderEdit" /&gt;
                    &lt;ItemTemplate&gt;
  
                           &lt;asp:Button ID="Btnapptid" runat="server" Text=""  CssClass="btnGVEdit"  CommandName="FillStaffTables" CommandArgument="&lt;%# CType(Container,GridViewRow).RowIndex %&gt;"/&gt;
  
                    &lt;/ItemTemplate&gt;
            &lt;/asp:TemplateField&gt;
  
       &lt;/Columns&gt;
   &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And the relevant VB code: </p>
  
  <pre><code>Private Sub Staffgv_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Staffgv.RowDataBound
      Select Case e.Row.RowType
          Case DataControlRowType.DataRow
              Dim row = DirectCast(e.Row.DataItem, DataRowView)
              Dim apptid As Integer = Integer.Parse(row("AppointmentID").ToString)
              Dim Btnapptid = DirectCast(e.Row.FindControl("Btnapptid"), Button)
              'Btnapptid.Text = apptid
              Btnapptid.ToolTip = apptid
      End Select
  End Sub
  
  Protected Sub Staffgv_RowCommand(ByVal sender As Object, e As GridViewCommandEventArgs)
      If (e.CommandName = "FillStaffTables") Then
          Dim index As Integer = Convert.ToInt32(e.CommandArgument)
  
          lblTxtTester.Text = "AppointmentID"
          TextboxTester.Text = index
      End If
  End Sub
  </code></pre>
  
  <p>If anyone would like more code like the code used to fill the GridView I'll post it, just didn't want to post too much irrelevant code.</p>
  
  <p>Thanks</p>