Jim Tat

Jim Tat

  • NA
  • 52
  • 47.2k

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

Nov 3 2019 7:46 AM

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)

But what I get is that my combobox is filling all the data not only what I need 

The code I tried:

  1. using (MySqlDataAdapter sda = new MySqlDataAdapter(@"SELECT DISTINCT Name ,CONCAT(Quantite ,'PCS -' ,Date) as Conc FROM articles", MyConnexion))  
  2.         {  
  3.             DataTable dt = new DataTable();  
  4.             sda.Fill(dt);  
  5.             DTG_Bordereau.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);  
  6.             DTG_Bordereau.Columns[0].DataPropertyName = "Name";  
  7.   
  8.             DTG_Bordereau.Columns[3].Width = 300;  
  9.   
  10.             DTG_Bordereau.DataSource = dt;  
  11.   
  12.             for (int i = 0; i < dt.Rows.Count; i++)  
  13.                   {  
  14.                 var val = dt.Rows[i]["Conc"].ToString();  
  15.   
  16.                 //check if it already exists  
  17.                 if (!QuantiteDisponible.Items.Contains(val))  
  18.                 {  
  19.                     QuantiteDisponible.Items.Add(val);  
  20.                 }  
  21.   
  22.             }  

Answers (1)