Set existing value in combo box C#

Asked

Viewed 121 times

1

I am passing a completed DTO object to an editing form, through the event click on a Change button.

In this edition form, I want all fields to be filled out as soon as it is opened, but the fields that are combo box are not being filled in the others yes.

private void btnAlterar_Click(object sender, EventArgs e)
    {
        dto.Id = (int)DgvCadListReceita.CurrentRow.Cells[0].Value;
        dto.Descricao = DgvCadListReceita.CurrentRow.Cells[1].Value.ToString();
        dto.Valor = Convert.ToDouble(DgvCadListReceita.CurrentRow.Cells[2].Value);

        dto.CategoriaReceita = (int)DgvCadListReceita.CurrentRow.Cells[3].Value;
        dto.DescCategoria = DgvCadListReceita.CurrentRow.Cells[4].Value.ToString();

        dto.Conta = (int)DgvCadListReceita.CurrentRow.Cells[5].Value;
        dto.DescConta = DgvCadListReceita.CurrentRow.Cells[6].Value.ToString();

        dto.DataVencimento = (DateTime)DgvCadListReceita.CurrentRow.Cells[7].Value;
        dto.Observacao = DgvCadListReceita.CurrentRow.Cells[8].Value.ToString();

        FrmCadReceita frm = new FrmCadReceita(dto);
        frm.ShowDialog();
        CarregarGrid();
    }

On the completed DTO form

 public FrmCadReceita(ReceitaDTO dto)
    {
        InitializeComponent();

        txtDescricaoReceita.Text = dto.Descricao;
        txtValorReceita.Text = Convert.ToString(dto.Valor);

        cboCategoriaReceita.Text = dto.DescCategoria.ToString();
        cboConta.Text = dto.DescConta;

        dtpDataVencimentoReceita.Text = dto.DataVencimento.ToString();
        txtObservacaoReceita.Text = dto.Observacao;
    }

inserir a descrição da imagem aqui How do I get the combobox to start with the value of the description in the DTO object? in this case I am passing the string type value, but I have the idCategory DTO filled in as well

1 answer

1

The fields were not being loaded due to not initializing the combo, I created a procedure to load the combo, then called it in a condition within the Load event

private void CarregarCombos()
    {
        cboCategoriaReceita.DataSource = catBll.Exibir();
        cboCategoriaReceita.DisplayMember = "des_categoria_receita";
        cboCategoriaReceita.ValueMember = "id";

        cboConta.DataSource = contBll.Exibir();
        cboConta.DisplayMember = "desc_conta";
        cboConta.ValueMember = "id";
    }

private void FrmCadReceita_Load(object sender, EventArgs e)
    {
        if (this.dto != null)
        {
            txtDescricaoReceita.Text = dto.Descricao;
            txtValorReceita.Text = Convert.ToString(dto.Valor);

            CarregarCombos();
            cboCategoriaReceita.SelectedValue = dto.CategoriaReceita;
            cboConta.SelectedValue = dto.Conta;

            dtpDataVencimentoReceita.Text = dto.DataVencimento.ToString();
            txtObservacaoReceita.Text = dto.Observacao;
        }
        else
        {
            LimparCampos();
        }
    }

Browser other questions tagged

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