How to add a column with text in gridview by code Behind?

Asked

Viewed 118 times

1

I make a SELECT in the bank and mount my gridview, after that I make another SELECT and needed to add an extra column with the values that came.

I have a Foreach to go through all the lines of the second SELECT, and another Foreach to go through all gridview lines. If the course name exists in gridview, you would have to add this new column, with the course value (R$).

I’m trying like this, no mistake, but also not shown the new column.

List<Informacoes> list2 = dal.ValorCurso(candidato);

          list2.ForEach(delegate (Informacoes dom)
          {
            string local = list2[dom.INDEX].CURSO;

            foreach (GridViewRow row in this.gvInscricoesCurso.Rows)
            {
              if (local.Contains(row.Cells[0].Text))
              {
                TemplateField template = new TemplateField();
                template.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                template.HeaderText = "Valor do curso";

                gvInscricoesCurso.Columns.Add(template);
              }
            }
          });

Someone would have a tip on how to do it or else another way?

To load the first time I do as follows, call for consultation and mounting of gridview:

Informacoes informacoes = new Informacoes();

Candidato candidato = new Candidato
{
CodColigada = Convert.ToInt16(ddlInstituicao.SelectedValue.Split('-')[0]),
CodFilial = Convert.ToInt16(ddlInstituicao.SelectedValue.Split('-')[1]),
};

DAL dal = new DAL();

DataTable dtInscritosCurso = dal.TotalInscritosCursoDetalhe(candidato);
gvInscricoesCurso.DataSource = dtInscritosCurso;
gvInscricoesCurso.DataBind();

And my query at the bank has enough lines, it would be too extensive to put in question, but is not returning any error.

  • You do like to carry the first time, I already see problems in the air ... ?

  • @Virgilionovic edited the question with the way I first carry

  • 1

    You need to do all this in SQL ...

  • @Virgilionovic Putz, the worst is that my query that makes the first call has 246 and is taking too long to load, so I wanted to do it this other way..

  • 249 call what?

  • @Virgilionovic query has 246 lines

Show 1 more comment

1 answer

1


The really ideal way would be for it not to be handled on its interface, but rather to make its SELECT own a JOIN which will also receive this value. As another option, try instead of populating the grid with the value, to have a value field in your candidate object. If you do not have the value field, only keep null.

  • Putz, worse is that my query that makes the first call has 246 and is taking too long to load, so I wanted to do this other way..

Browser other questions tagged

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