Replacing Checkbox with a String

Asked

Viewed 53 times

0

I need to show off "Active" or "Inactive" in the column Situation of Datagridview, how to do ? inserir a descrição da imagem aqui

This "Situation" Column is of the Boolean type:

inserir a descrição da imagem aqui

This is the code where I fill out the Datagridview:

private void PreencherDataGridView()
        {
            try
            {
                ConvenioModel convenio = new ConvenioModel();
                convenio.STATUS = rdbFiltroAtivo.Checked;
                dgvConvenio.DataSource = ConvenioNegocio.ListarConvenioMedico(convenio);

                dgvConvenio.Columns["ID_CONVENIO_MEDICO"].Visible = false;                    
                dgvConvenio.Columns["CNPJ"].Width = 80;
                dgvConvenio.Columns["RAZAO_SOCIAL"].Width = 150;
                dgvConvenio.Columns["NM_FANTASIA"].Width = 150;
                dgvConvenio.Columns["DT_CADASTRO"].Width = 50;
                dgvConvenio.Columns["DT_ULT_ATUALIZACAO"].Width = 50;
                dgvConvenio.Columns["STATUS"].Width = 30;

                dgvConvenio.Columns["RAZAO_SOCIAL"].HeaderText = "RAZÃO SOCIAL";
                dgvConvenio.Columns["NM_FANTASIA"].HeaderText = "NOME FANTASIA";
                dgvConvenio.Columns["DT_CADASTRO"].HeaderText = "DT CADASTRO";
                dgvConvenio.Columns["DT_ULT_ATUALIZACAO"].HeaderText = "ATUALIZAÇÃO EM";
                dgvConvenio.Columns["STATUS"].HeaderText = "SITUAÇÃO";

                dgvConvenio.Columns["CNPJ"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvConvenio.Columns["DT_CADASTRO"].DefaultCellStyle.Format = "dd/MM/yyyy";
                dgvConvenio.Columns["DT_ULT_ATUALIZACAO"].DefaultCellStyle.Format = "dd/MM/yyyy";
                dgvConvenio.Columns["CNPJ"].DefaultCellStyle.Format = "00.000.000/0000-00";


                dgvConvenio.ScrollBars = ScrollBars.Both;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
  • 1

    You can change the column type to text and the data type (returned in ConvenioNegocio.ListarConvenioMedico) for string.

  • 1

    Incidentally, capturing an exception just to launch a new one with the same message doesn’t make any sense. This way, you lose the origin of the problem and still force Runtime to make the unnecessary release of an exception.

  • 1

    an option is to create a property in the model to represent it: public string StatusNome { {get{ return this.Status ? "Ativo" : "Inativo"; } }

  • 2

    It is a much better option, by the way. Just a note: it could be written like this: public string StatusNome => Status ? "Ativo" : "Inativo"

  • @LINQ thanks for the remark I changed my code and left only throw;, catch{ throw; }.

  • 1

    can even take out the Try catch... the exception will propagate in the same way...

Show 1 more comment

1 answer

1


I’m going to assume that your object carried on DataGridView be the ConvenioNegocio. Then, you create a read-only property that will display the value as string:

public class ConvenioNegocio
{
    //...outras propriedades
    public bool Status {get;set;}
    public string Situacao => Status ? "Ativo" : "Inativo;
}

Now you will have an extra column of Datagridview to represent the property Situacao, just hide the STATUS:

 dgvConvenio.Columns["STATUS"].Visible = false;

 dgvConvenio.Columns["SITUACAO"].HeaderText = "Situação";

Ps. I don’t know how you are generating the columns, so it’s just an example.

Browser other questions tagged

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