0

I'm currently working on a C# desktop app for a school project, I'm a student and henceforth, a beginner. I have an issue which is giving me a lot a trouble.

I load a list of object (From DB) into a DatagridView via .datasource . So far no specific problem. Each object of the list has a color assigned to it.

The Column Type is set to DataGridViewComboBoxColumn in visual studio properties. (See settings in following code)

Once on the DGV, when I click on the color cell of any row of the color column, I have the full list of static data displayed. I can select any color of the list. However if I hit enter or leave the row, the initial color comes back...

Exemple: The color from DB is "Red", I select "Yellow" from the combo, hit enter, the cell reverts back to "Red"...

What am I missing?

Thanks for any help!

Here is some usefull sample of the code:

public class EplfColor
    {
        public int eplfColorId{ get; set; }

        public string eplfColorName { get; set; }

        public int eplfColorCode { get; set; }

        public override string ToString()
        {
            return this.eplfColorName;
        }
    }
 private void RefreshDGV()
        {
            combocolumn.DataSource = SitacController.GetEplfColor();

            this.DGV_SITAC.DataSource = SitacController.GetAllSitac();
        }

Extract of the Visual Studio Designer:

 // 
            // EPLF_Color
            // 
            EPLF_Color.DataPropertyName = "EplfColor";
            EPLF_Color.DataSource = eplfColorBindingSource;
            EPLF_Color.DisplayMember = "eplfColorName";
            EPLF_Color.HeaderText = "EPLF Color";
            EPLF_Color.MinimumWidth = 6;
            EPLF_Color.Name = "EPLF_Color";
            EPLF_Color.Resizable = DataGridViewTriState.True;
            EPLF_Color.SortMode = DataGridViewColumnSortMode.Automatic;
            EPLF_Color.ValueMember = "eplfColorId";
            // 
            // eplfColorBindingSource
            // 
            eplfColorBindingSource.DataSource = typeof(Models.EplfColor);

ComboBoxList

I was expecting the cell value to change.

  • See this link on how to add a datagridviewcomboboxcolumn to a dgv during runtime: https://www.c-sharpcorner.com/UploadFile/9f4ff8/add-combobox-and-checkbox-into-the-datagrdiview-in-C-Sharp/ – SoftwareDveloper Apr 13 '23 at 14:27

1 Answers1

0

DataGridViewComboBoxCell must be placed inside a DataGridViewComboBoxColumn, have you properly builded the DataGridView?

Also, instead of creating a new cell, try modifing the one that already exist

DataGridViewComboBoxCell gridCombo = this.DGV_SITAC[e.ColumnIndex, e.RowIndex];
Renan Ruan
  • 136
  • 7
  • Hello Ruan, I did as you proposed. The column is now correctly set as a ComboBoxColumn and I also implemented the modification of the current cell. Unfortunately, still no luck. Each time I select a color, it reverts to the initial one... – Le_Fossoyeur Apr 13 '23 at 14:51
  • Are you calling DGV_SITAC_CellClick() on every click in the cell? If you are, it might be related to the problem, since you will be recreating the cell on every click and it will return back to the default values. – Renan Ruan Apr 13 '23 at 15:20
  • I did, i removed this event and now all is managed at column level. I will modify the thread to show the current status of my code. – Le_Fossoyeur Apr 13 '23 at 15:34
  • I'd recommend you to create the CellValueChanged, CellValidated and DataMemberChanged events of the dataGridView and add thread watching points to them, so you can debbug your code and see whether something is changing the cells selected value or not – Renan Ruan Apr 14 '23 at 13:22