c# – How to add a linked column in GridView?

Question:

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 excerpt grdListaAlteracoes.Columns.Add(linkColumnBoundField); I add a column to the grid, but I don't know how to populate each field with the link I want.

I thought of assembling the HTML directly in the initial list that assembles the Grid, but I did a test and it doesn't encode the HTML and the text is shown with the tags instead of the link.

How can I add this column with with the links?

Answer:

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

Here is the code with the solution:

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);
    }
}

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

protected void grdListaAlteracoes_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[5].Visible = false;
}
Scroll to Top
AllEscort