title: How to get row index according to clicked button from asp.net GridView?
body: <p>I have Gridview with some records (1 record=1 row).</p>
  
  <p>On every row I added a button to delete this record from my mysql db.</p>
  
  <p>Every row has the same button.</p>
  
  <p>The problem is I need to know <strong>in which row the button was clicked</strong>? I need this to get a row index to get id of my record which is in that row. </p>
  
  <p>How can I do this in easiest way?</p>
  
  <p>Gridview:</p>
  
  <pre><code>        &lt;asp:GridView ID="GridView1" runat="server" 
          CellPadding="6" EnableModelValidation="True" ForeColor="#333333" 
          GridLines="None" Caption="TWOJE WIZYTY" Font-Bold="True" 
          onrowcreated="GridView1_RowCreated" style="text-align: left"&gt;
          &lt;AlternatingRowStyle BackColor="#AEAEAE" /&gt;
          &lt;EditRowStyle BackColor="Blue" /&gt;
          &lt;FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /&gt;
          &lt;HeaderStyle BackColor="#868686" Font-Bold="True" ForeColor="White" /&gt;
          &lt;PagerStyle BackColor="Blue" ForeColor="#333333" HorizontalAlign="Center" /&gt;
          &lt;RowStyle BackColor="#C7C7C7" ForeColor="#333333" /&gt;
          &lt;SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The button is being added like that:</p>
  
  <pre><code>        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
      {
          Button b1 = new Button();
          b1.Text = "usuń";
          b1.OnClientClick = "return potwierdzenie()";
          b1.Click+=new EventHandler(b1_Click);
          TableCell cel = new TableCell();
          cel.Width = Unit.Pixel(180);
          if (e.Row.RowType == DataControlRowType.Header)
          {
              e.Row.Cells[0].Visible = false;
              e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
              e.Row.Cells[2].Text = "";
              e.Row.Cells.Add(cel);
          }
          else
          {
              //HERE IS MY BUTTON ADDED! *********************
              cel.Controls.Add(b1);
              cel.HorizontalAlign = HorizontalAlign.Right;
              e.Row.Cells[0].Visible = false;
              e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
              e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
              e.Row.Cells.Add(cel);
          }
      }
  </code></pre>