title: Dynamic link in asp.net gridview
body: <p>I'm trying to create a gridview with an extra column that contains a hyperlink for each row. Unfortunately my hyperlinks doesn't start from the top of the column, instead they are started from the second row of the hyperlink's column.</p>
  
  <p>See this picture for more information >>> <a href="http://i.imgur.com/TLsVo5s.png" rel="nofollow">http://i.imgur.com/TLsVo5s.png</a></p>
  
  <p>As you can see in that picture, there is a 'view' column that contains hyperlinks, but the problem is the first row is always empty. The hyperlink that's on the second row should be on the first, and the third should be on the second, and so on.</p>
  
  <p>Can anyone show me where I went wrong?</p>
  
  <p>Here is my gridview declaration on my aspx page:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" CellPadding="4" AllowPaging="True" 
      AllowSorting="True" AutoGenerateColumns="False" 
      OnPageIndexChanging="GridView1_PageIndexChanging" 
      OnSorting="GridView1_Sorting" PageSize="20" DataKeyNames="no_kwitansi"
      DataSourceID="home1" BackColor="White" BorderColor="#CC9966" BorderStyle="None"
      BorderWidth="1px" RowStyle-Wrap="False" OnRowDataBound="GridView1_RowDataBound"&gt;
      &lt;AlternatingRowStyle BackColor="#CCCCCC" /&gt;
      &lt;FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /&gt;
      &lt;HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /&gt;
      &lt;PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /&gt;
      &lt;RowStyle BackColor="White" ForeColor="#330099" /&gt;
      &lt;RowStyle HorizontalAlign="Center" /&gt;
      &lt;SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#FEFCEB" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#AF0101" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#F6F0C0" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#7E0000" /&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField HeaderText="#" /&gt;
          &lt;asp:BoundField DataField="no_kwitansi" HeaderText="No.Kwitansi" SortExpression="no_kwitansi" ReadOnly="True" /&gt;
          &lt;asp:BoundField DataField="nama_vendor" HeaderText="Vendor" SortExpression="nama_vendor" /&gt;
          &lt;asp:BoundField DataField="nama_pekerja" HeaderText="Pekerja" SortExpression="nama_pekerja" /&gt;
          &lt;asp:BoundField DataField="nama_penanggungjawab" HeaderText="Penanggungjawab" SortExpression="nama_penanggungjawab" /&gt;
          &lt;asp:BoundField DataField="satuan" HeaderText="Satuan" SortExpression="satuan" /&gt;
          &lt;asp:BoundField DataField="jumlah" HeaderText="Nominal" SortExpression="jumlah" /&gt;
          &lt;asp:BoundField DataField="tanggal" HeaderText="Tanggal" SortExpression="tanggal" /&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Here is my C# code behind:</p>
  
  <p>This is my page_load function, I created the template field here.</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              ViewState["SortExpr"] = Sort_Direction;
  
              TemplateField tfield = new TemplateField();
              tfield.HeaderText = "View";
              GridView1.Columns.Add(tfield);
  
              home1.DataBind();
          }
      }
  </code></pre>
  
  <p>Here is my gridview rowDataBound function, where I create the hyperlink.</p>
  
  <pre><code>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              HyperLink hlContro = new HyperLink();
              for (int i = 0; i &lt; GridView1.Rows.Count; i++)
              {
                  hlContro.NavigateUrl = "./Home.aspx?ID=" + GridView1.Rows[i].Cells[1].Text;
                  //hlContro.ImageUrl = "./sample.jpg";
                  hlContro.Text = "Documents";
                  //GridView1.Rows[i].Cells[0].Controls.Add(hlContro);
              }
              e.Row.Cells[8].Controls.Add(hlContro);
          }
      }
  </code></pre>