merging gridview rows does not working with controls inside templatefield itemtemplate
<p>I have one gridview with columns like checkbox, companyname with hyperlink(templatefield used), branchname(templatefield used) with hyperlink, address etc. Here i am displaying comapny name &amp; its branch name, but i want to merge company for many branches as my grid showing rows with the same company for many branches. I have tried but its not working with the templatefield items as i have hyperlink to companyname field to redirect view page.
I used following grid-code in .aspx page:</p>

<pre><code>&lt;asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" CellPadding="4"
CellSpacing="1" PageSize="15" Width="100%" Visible="true" BorderStyle="Solid"
BorderWidth="1px" OnDataBound="grid1_DataBound" DataSourceID="SqlDS1"&gt;
&lt;Columns&gt;
    &lt;asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"&gt;
        &lt;HeaderTemplate&gt;
        &lt;/HeaderTemplate&gt;
        &lt;ItemTemplate&gt;
            &lt;asp:CheckBox ID="chk1" runat="server" ToolTip='&lt;%# Bind("ID") %&gt;' /&gt;
        &lt;/ItemTemplate&gt;
        &lt;ItemStyle HorizontalAlign="Center" Width="20px" /&gt;
    &lt;/asp:TemplateField&gt;
    &lt;asp:TemplateField HeaderText="Company Name" SortExpression="CompanyName" ItemStyle-HorizontalAlign="Center"
        ItemStyle-Width="90px" ItemStyle-VerticalAlign="Middle"&gt;
        &lt;ItemTemplate&gt;
            &lt;asp:HyperLink ID="hypCompanyName" Font-Underline="false" SkinID="SimpleLink" runat="server"
                Text='&lt;%# Bind("CompanyName") %&gt;' NavigateUrl='&lt;%# "~/viewCompany.aspx?io=" + DataBinder.Eval (Container.DataItem,"ID").ToString() %&gt;'&gt;&lt;/asp:HyperLink&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;asp:TemplateField HeaderText="Branch Name" SortExpression="BranchName" ItemStyle-HorizontalAlign="Center"
        ItemStyle-Width="90px" ItemStyle-VerticalAlign="Middle"&gt;
        &lt;ItemTemplate&gt;
            &lt;asp:HyperLink ID="hypBranchName" Font-Underline="false" SkinID="SimpleLink" runat="server"
                Text='&lt;%# Bind("BranchName") %&gt;' NavigateUrl='&lt;%# "~/ViewBranch.aspx?ioB=" + DataBinder.Eval (Container.DataItem,"IDB").ToString() %&gt;'&gt;&lt;/asp:HyperLink&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address"
        ItemStyle-HorizontalAlign="Center" /&gt;
&lt;/Columns&gt;
</code></pre>

<p></p>

<p>And i have written following code in .cs page:</p>

<pre><code>protected void grid1_DataBound(object sender, EventArgs e)
{

    for (int rowIndex = grid1.Rows.Count - 2;
                                 rowIndex &gt;= 0; rowIndex--)
    {
        GridViewRow gvRow = grid1.Rows[rowIndex];
        GridViewRow gvPreviousRow = grid1.Rows[rowIndex + 1];
        for (int cellCount = 0; cellCount &lt; gvRow.Cells.Count;
                                                      cellCount++)
        {
            if (gvRow.Cells[cellCount].Text != "" || gvPreviousRow.Cells[cellCount].Text != "")
            {
                if (gvRow.Cells[cellCount].Text != "&amp;nbsp;" || gvRow.Cells[cellCount].Text != "&amp;nbsp;")
                {
                    if (gvRow.Cells[cellCount].Text ==
                                           gvPreviousRow.Cells[cellCount].Text)
                    {
                        if (gvPreviousRow.Cells[cellCount].RowSpan &lt; 2)
                        {
                            gvRow.Cells[cellCount].RowSpan = 2;
                        }
                        else
                        {
                            gvRow.Cells[cellCount].RowSpan =
                                gvPreviousRow.Cells[cellCount].RowSpan + 1;
                        }
                        gvPreviousRow.Cells[cellCount].Visible = false;
                    }
                }
            }
            else
            {
                gvRow.Cells[cellCount].RowSpan = 0;
                gvPreviousRow.Cells[cellCount].RowSpan = 0;
            }
        }
    }
}
</code></pre>

<p>But its not merging same companies showing all branches when i used templatefield items, when i removed templatefield items its finely works with boundfield items.
So please help me to resolve this issue with templatefield items</p>
