Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How can I fill a Combobox in a datagridview from db mysql

$
0
0

Hello, I have a datagridview  which has a combobox.

I want to populate this comboboxes at runtime. But I don't know how.

In the database I have: 

Column1 : Name               Column2 : Quantity                          Column 3 : Date


a                                                 1                                                         2019
b                                                 2                                                         2018
c                                                 3                                                          2017
a                                                 4                                                          2015

So what I need is to show in my combobox for Name a : (1 - 2019)

                                                                                         (4 - 2015)

These two must be filled in the combobox.

For Name b the combobox will be : (2 - 2018)     

The code I tried:

using (MySqlDataAdapter sda = new MySqlDataAdapter(@"SELECT DISTINCT Name FROM articles", MyConnexion))  
        {  
            DataTable dt = new DataTable();  
            sda.Fill(dt);  
            DTG_Bordereau.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);  
            DTG_Bordereau.Columns[0].DataPropertyName = "Name";  
            DTG_Bordereau.Columns[3].Width = 300;  
            DTG_Bordereau.DataSource = dt;  
                try  
                {  
                    for (int i = 0; i < dt.Rows.Count; i++)  
                    {  
                        QuantiteDisponible.Items.Clear();  
                    MySqlDataAdapter sda2 = new MySqlDataAdapter("SELECT DISTINCT CONCAT(Quantite ,'PCS -' ,Date) as Conc FROM articles where Name='" + dt.Rows[i][0].ToString() + "'", MyConnexion);  
                    DataTable dt2 = new DataTable();  
                    sda2.Fill(dt2);  
                    QuantiteDisponible.DataSource = dt2;  
                    QuantiteDisponible.DisplayMember = "Conc";  
                    QuantiteDisponible.ValueMember = "Conc";  
                }  
            }  
                catch  
                { }  
        }  
So with this code, my first combobox is correct, but the other comboboxes are taking the same value of the first combobox....


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>