Load Combobox with concatenated string

Asked

Viewed 287 times

0

Code that loads the comboBox:

private void frmAdicionarProdutos_Load(object sender, EventArgs e)
        {
             this.Load += new System.EventHandler(this.frmAdicionarProdutos_Load);
             string serverName = "localhost";
             string port = "5432";
             string userName = "postgres";
             string password = "adm";
             string databaseName = "GE";
             NpgsqlConnection conn = null;
             string ConnString = null;

             ConnString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                           serverName, port, userName, password, databaseName);

            using (conn = new NpgsqlConnection(ConnString))
            {
                conn.Open();

                string cmdCarregar = String.Format("SELECT CONCAT(id_produto,' ', nome,' ', preco) FROM PRODUTOS;");
                using (NpgsqlCommand cmd = new NpgsqlCommand(cmdCarregar, conn))
                {
                    NpgsqlDataReader dr = cmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(dr);

                    cbProdutos.DisplayMember = "nome";
                    cbProdutos.ValueMember = "id_produto";
                    cbProdutos.DataSource = dt;
                    conn.Close();
                }     
            }
        }

I rode the select to concatenate what I need to show in the combobox, but I don’t know how to inform in the DisplayMember. The way it is, when I run the program it loads the combobox with the following value: System.Data.DataRowView.

  • Gee, young man. I want to help you, but there’s so much bad in there that I don’t even know where to start.

  • kkk I am aware of this.. you are not the first to say this :( but help me there.. because the select is correct, tested in the bank.. just in time to show that you’re fucked up..

  • 1

    then... this.Load += new System.EventHandler(this.frmAdicionarProdutos_Load); inside the event itself I’ve never seen...it’s good that the load only runs once right... on the other 'bad' things I will consider that it is only your tests and that this will not go to the final result (hopefully) hehe vlw

  • 1

    It is not duplicated, there was doubt of how to load, here is doubt of how to show the concatenated items..

1 answer

2


Come on, come on... you have to bring the columns in your select, and you don’t even need String.Format :

 string cmdCarregar = "SELECT CONCAT(id_produto,' ', nome,' ', preco) as descricao, id_produto FROM PRODUTOS;";

Notice that I am assembling a concatenated column, and still bringing the id_product in another column.

using (conn = new NpgsqlConnection(ConnString))
{
    conn.Open();

    string cmdCarregar = "SELECT CONCAT(id_produto,' ', nome,' ', preco) as descricao, id_produto FROM PRODUTOS;";

    using (NpgsqlCommand cmd = new NpgsqlCommand(cmdCarregar, conn))
    {
        NpgsqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);

        cbProdutos.DisplayMember = "descricao";
        cbProdutos.ValueMember = "id_produto";
        cbProdutos.DataSource = dt;
        conn.Close();
    }     
}

Finally, just put in Displaymember, the name of the column that was created in the query, the one that came concatenated: descricao

The rest stays the same.

hope I’ve helped

  • That’s what I needed, you know how I put more spaces between the fields? Just go putting the ' '

  • 1

    yes, particularly, would put another separator...there is preference =] CONCAT(id_produto,'-', nome,' | R$', preco)

  • 1

    True, it made more sense! Thank you.

Browser other questions tagged

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