0
votes

I have the following GridView with information columns and a Detail Hyperlink at the end of the row:

<cml:DataGrid ID="gvDados" DataKeyNames="Codigo_da_Agencia, Codigo_Do_Agente" runat="server" AutoGenerateColumns="False" AllowPaging="false"
                SkinID="datagridSkin" TotalRecords="0" OnSelectedIndexChanged="gvDados_SelectedIndexChanged"
                OnPageIndexChanging="gvDados_PageIndexChanging" HeaderStyle-Wrap="false">
                <Columns>
                    <asp:BoundField DataField="Codigo_Do_Agente" HeaderText="Codigo_Do_Agente" SortExpression="Codigo_Do_Agente" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Codigo_Da_Agencia" HeaderText="Codigo_Da_Agencia" SortExpression="Codigo_Da_Agencia" ItemStyle-Wrap="false" />
                    <asp:BoundField DataField="Nome_Do_Contato" HeaderText="Nome_Do_Contato" SortExpression="Nome_Do_Contato" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Nome_Da_Agencia" HeaderText="Nome_Da_Agencia" SortExpression="Nome_Da_Agencia" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Telefone" HeaderText="Telefone" SortExpression="Telefone" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Numero" HeaderText="Numero" SortExpression="Numero" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Codigo_Do_Logradouro" HeaderText="Codigo_Do_Logradouro" SortExpression="Codigo_Do_Logradouro" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Apartamento" HeaderText="Apartamento" SortExpression="Apartamento" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Complemento" HeaderText="Complemento" SortExpression="Complemento" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="Codigo_Do_Bairro" HeaderText="Codigo_Do_Bairro" SortExpression="Codigo_Do_Bairro" ItemStyle-Wrap="false"/>
                    <asp:BoundField DataField="DTHR" HeaderText="DTHR" SortExpression="DTHR" ItemStyle-Wrap="false"/>
                    <asp:HyperLinkField DataTextField="Codigo_Da_Agencia" DataTextFormatString="Detalhe" DataNavigateUrlFields="Codigo_Da_Agencia, Codigo_Do_Agente" 
                        HeaderText="Detalhes" DataNavigateUrlFormatString="../../Controls/Detalhes/Agencia.aspx?codigoAgencia={0}&codigoAgente={1}" Target="_blank"  />
                </Columns>
            </cml:DataGrid>

Everything works fine, when the hyperlink is clicked it opens a new tab. But my problem is that the row is also selected.

I want to know if there is a way to prevent the row to be selected when the hyperlink is clicked. Or if there is a way to verify on the method PageIndexChanging in codebehind that was the last column that was clicked.

Thanks in advance!

2
is there a RowSelection or RowSelected property that can be set on the GridView..? - MethodMan
@DJKRAZE I don't see anything like it here - Christopher Freyburg
@ChristopherFreyburg Instead of HyperLinkField, try using a LinkButton inside TemplateField. - Bharadwaj

2 Answers

0
votes

You can cancel the selected index when the user clicks a particular column in the SelectionIndex_Changing event as given in the msdn example for the SelectedRow. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.100).aspx

0
votes

Found out the solution, I must say is quite confusing after a lot of desperate tries...

I have a custom class that extends original GridView, then I override the Render() to be like this:

protected override void Render(HtmlTextWriter writer)
    {
        foreach (GridViewRow row in this.Rows)
            if (row.RowType == DataControlRowType.DataRow)
                foreach (DataControlFieldCell cell in row.Cells)
                    if ((cell.ContainingField).HeaderText != "Detalhes")
                        cell.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this, string.Format("Select${0}", row.RowIndex), true);

        base.Render(writer);
    }

foreach cell I'll verify if the Header name is the detail one. If it isn't I'll add a onclick atribute

I honestly don't think its pretty, but at least it's working :P