Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am quite new to C# and I am trying to populate fill several textboxes with data from a selected combobox.
I have my main window with the textboxes and comboboxes and a separate class for the connection to the database (I am using XAMPP/PhpMyAdmin).
I managed to fill the comboboxes with the data from the database, but I cannot fill the texboxes from the selected combobox.
I checked other questions and tutorials, but all I managed to achieve is to get the primary key into the text box, but I need different columns from the table, depending on the textbox.

What I have tried:

I populated the combobox from the database:

void Completez_Combo_Furnizor()
        {
            combo_furnizor = DB_Furnizori.Combo_Furnizor();
            comboBoxFurnizor.Items.Clear();
            comboBoxFurnizor.DataSource = combo_furnizor;
            comboBoxFurnizor.ValueMember = "id_furnizor";
            comboBoxFurnizor.DisplayMember = "nume";
            

        }


I double clicked on the combobox and wrote the following, but all I can get is the primary key (the first column). In the textbox, I need to get the 7th column (which is a double type.

private void comboBoxFurnizor_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxPret.Text = comboBoxFurnizor.SelectedItem.ToString();
        }


And this is from the database class (DB_Furnizori.cs), where I open the connection and have multiple queries for the database.

public static DataTable Combo_Furnizor()
        {
            conn.Open();
            MySqlCommand comboFurnizor = new MySqlCommand("SELECT * from furnizori ORDER BY nume", conn);
            MySqlDataAdapter adaptc = new MySqlDataAdapter(comboFurnizor);
            DataTable combo_furnizori = new DataTable();
            adaptc.Fill(combo_furnizori);
            conn.Close();
            return combo_furnizori;


        }


Please help, I don't know how to make it work :(
Posted
Updated 17-Jun-19 6:57am
Comments
Richard MacCutchan 17-Jun-19 4:12am    
You need to use the item from the combobox to find the relevant row in the datatable. You can then extract the data in whichever column you need.
vladj91 17-Jun-19 5:03am    
Thank you for the quick answer. I don't really know how to do that. I tried to search for this, but I cannot find anything that would help me

1 solution

Why do you use a combobox. Isn't it better to use a datagridview?

Fill your datagridview:

C#
datagridview.datasource =  Combo_Furnizor();


And then:

C#
private void DataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0) return;

            var r = dataGridView1.CurrentRow;
            if (r != null)
            {
                var rid = r.Cells["id_furnizor"].Value.ToString();
                var columnvalue = r.Cells["yourcolumnname"].Value.ToString();

                textBox1.Text = rid + "; " + columnvalue;
            }
        }
 
Share this answer
 
v2
Comments
Maciej Los 17-Jun-19 15:14pm    
Please, use proper formatting.

BTW: what's the difference between using combobox and datagridview?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900