0

I have a datagridview which is filled with this.adapter.Fill(this.addressesDataSet);. In the grid, one column is combobox which should display a list of cities for a given address.

The database contains data:

Partner_Address

id     |  address|    city_id   |  ....
----------------------------------------
1      | asd     |       5      | 

City

id     |  City    |  
------------------
1      | New York |  
2      | London   | 

The city_id column contains the foreign key id that is returned to the grid.

The problem is how to select a specific item in the combobox to display the name of the city for a specific row from the database. The database only returns the ID from the city but not the name.

I initialize grid with this

 public void InitializePartnerAddressesList(int id)
        {
            using (this.conn = new MySqlConnection(DB.connString))
            {
                try
                {
                    conn.Open();

                    string sql = "SELECT p.* FROM partneri_adrese p WHERE p.partner_id = @id and p.primarna";

                    this.adreseAdapter = new MySqlDataAdapter(sql, conn);
                    this.adreseAdapter.SelectCommand.Parameters.AddWithValue("id", id);
                    this.adreseAdapter.Fill(this.adreseDataSet);

                    this.adreseDataGridView.AutoGenerateColumns = false;
                    this.adreseBindingSource.DataSource = this.adreseDataSet.Tables[0];
                    this.adresBindingNavigator1.BindingSource = this.adreseBindingSource;
                    this.adreseDataGridView.DataSource = this.adreseBindingSource;                                       
                }
                catch(Exception e) {
                    MessageBox.Show(e.Message);
                }                             
            }
        }

When i run i get

enter image description here

Is this possible using an adapter or do I have to manually loop through each line and check it manually?

Ivan
  • 5,139
  • 11
  • 53
  • 86
  • Why did you post another question instead of adding details to the [previous one](https://stackoverflow.com/q/76846577/7444103)? -- Based on the information you have given, you have to load the table that contains the list of cities, use this *collection* (a DataTable on this side, I assume) and set it as the DataSource of a ComboBox Column (of Type `long`; probably prepending a value of `0` as the default, if you need to add new rows), before you set the DataSource of the DGV – Jimi Aug 06 '23 at 17:31

0 Answers0