C# – Get the hyperlink field value of the selected record in gridview

asp.netccheckboxgridview

My Gridview has a hyperlink field as on of its columns and for each row there are check boxes.We can select any record by selected the check box.

The problem is: I am not being able to get the hyperlink field record.

The code for doing this is:

 for (int i = 0; i < GridView1.Rows.Count; i++)
 {
   CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[1].FindControl("myCheckBox");

    if (cb != null && cb.Checked)
    {
      String Retailer = GridView1.Rows[i].Cells[0].Text.ToString();
    }
 } 

At Cells[0] my hyperlink field is present.All the other fields can be accesss by this code.When I change the code to GridView1.Rows[i].Cells[2].Text.ToString() , I can get the Column value at Cell[2] position which is not hyperlink field.

What changes am I suppose to make in order to get the hyperlink filed value.
Please help!!

Edit: The code for Gridview is:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"        DataKeyNames="Col1"
                    Width="100%"
                    BackColor="#F8ECE0"
                    BorderColor="#8B4513"
                    ShowFooter="false"
                    CellPadding="3"
                    CellSpacing="0"
                    Font-Name="Comic Sans"
                    Font-Size="11pt"
                    HeaderStyle-BackColor="#FFA500"
                    EnableViewState="false"
                    AutoPostBack="true">
                    <Columns>
                        <asp:HyperLinkField DataNavigateUrlFields="Col1" DataNavigateUrlFormatString="Col1.aspx?Name={0}" DataTextField="Col1" HeaderText="Col1" />
                        <asp:BoundField DataField="Col2" HeaderText="Col2" SortExpression="Col2" />
                        <asp:BoundField DataField="Col3" HeaderText="Col3" SortExpression="Col3" />
                        <asp:BoundField DataField="Col4" HeaderText="Col4" SortExpression="Col4" />
                        <asp:TemplateField >
                        <ItemTemplate>
                                <asp:CheckBox ID="myCheckBox"  HeaderText="Check to Cancel" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>

                    </Columns>
  </asp:GridView>

Best Answer

You need to get to the control type first and then the Text property, like this:

String Retailer = ((HyperLink)GridView1.Rows[i].Cells[0].Controls[0]).Text;