How to add a link column in Gridview?

Asked

Viewed 722 times

3

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

I thought about putting the HTML right in the initial list that mounts the Grid, but I did a test and it does not do the HTML Find and the text is shown with the tags instead of the link.

How can I add this column with links?

1 answer

1


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 on DataSource.

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

How I created a new field in the DataSource to generate the links from GridView, 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.