Search code examples
c#.netwinformsdatagridviewdatagridviewcomboboxcolumn

c# DataGridViewComboBoxColumn selectedValue spread throughout the whole line


I'm stuck with the following problem:

i build a dataGridView with 7 DataGridViewComboBoxColumn, adding with the following code

foreach (string jour in Enum.GetNames(typeof(Jours)))
        {
            DataGridViewComboBoxColumn comboboxColumn = CreateComboBoxColumn();
            List<Journee> myJourneeList = new List<Journee>(mydatas.journeeTypeList.ToList());
            comboboxColumn.DataSource = myJourneeList;
            comboboxColumn.DisplayMember = "name";
            comboboxColumn.ValueMember = "id";
            comboboxColumn.DataPropertyName = "id";
            comboboxColumn.HeaderText = jour;
            dataGridView_machineSemaines.Columns.Add(comboboxColumn);
            setSelectedValue(jour, myJourneeList);
        }

I get the following dataGridView :

enter image description here

then if i selected a new value in one combobox :

enter image description here

and if a i scroll (with the horizontal-scroll) (or if i open an other tab and come back to this tab), then the whole row get the same selectedValue :

enter image description here

how can i fix this problem?


Solution

  • You are using the same DataPropertyName for all columns. So the behavior is expected. In fact, all columns are showing a single value. The id property of your data source of grid.

    If you want them to show different values, use different DataPropertyName or in another word, bind them to different columns.