How to add a linked column in GridView?

2

I have the following code that generates my GridView:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                AlteracoesContrato alteracoes = new AlteracoesContrato(movimentacaoId);
                grdListaAlteracoes.DataSource = alteracoes.ListaResumidaAlteracoes;
                if (alteracoes.ListaResumidaAlteracoes.Count > 0)
                {
                    lblAviso.Visible = false;
                    BoundField linkColumnBoundField = new BoundField();
                    linkColumnBoundField.HeaderText = "Visualizar";
                    grdListaAlteracoes.Columns.Add(linkColumnBoundField);

                    grdListaAlteracoes.DataBind();
                }
                else
                    lblAviso.Text = "Nenhuma alteração foi realizada nesse contrato.";

            }
            catch (Exception ex)
            {
                lblAviso.Text = "Erro ao exibir alterações do contrato.";
                GravaLogErro(ex);
                ShowAlert(ERRO_RECUPERAR);
            }
        }

In the% wrapper I add a column to the grid, but I do not know how popular each field with the link I want.

I thought of putting the HTML right into the initial list that mounted the Grid, but I did a test and it does not encode the HTML and the text is shown with the tags instead of the link.

How can I add this column to the links?

    
asked by anonymous 31.03.2017 / 16:32

1 answer

0

I found the solution.

I needed to create a HyperLinkedFiled and not BoundField . After that, I just linked the column created with the field where I generated the links in DataSource .

The source code looks like this:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                AlteracoesContrato alteracoes = new AlteracoesContrato(movimentacaoId);
                grdListaAlteracoes.DataSource = alteracoes.ListaResumidaAlteracoes;
                if (alteracoes.ListaResumidaAlteracoes.Count > 0)
                {
                    lblAviso.Visible = false;  
                    //Adicionar como link os dados de Visulizar:
                    HyperLinkField linkColumnBoundField = new HyperLinkField();
                    linkColumnBoundField.HeaderText = "Visualizar";
                    linkColumnBoundField.DataTextField = "Visualizar";
                    grdListaAlteracoes.Columns.Add(linkColumnBoundField);

                    grdListaAlteracoes.DataBind();
                }
                else
                    lblAviso.Text = "Nenhuma alteração foi realizada nesse contrato.";

            }
            catch (Exception ex)
            {
                lblAviso.Text = "Erro ao exibir alterações do contrato.";
                GravaLogErro(ex);
                ShowAlert(ERRO_RECUPERAR);
            }
        }

Since I created a new field in DataSource to generate the GridView links, I needed to hide the column that was duplicated (without the link). For this I used the event RowDataBound :

protected void grdListaAlteracoes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[5].Visible = false;
        }
    
31.03.2017 / 19:16