0

First of all, I must mention that I have seen this question, but it didn't help me to fix my problem.

According to my previous question, I saved my DataGridView to an XML file. Now I am going to fill the DataGridView when I load the window form using the data stored on the XML file.

My problem is that when I want to set the value of one ComboBox based on the stored data, the other ComboBox's value changes too. I want to set each ComboBox's value separately.

My code is as follows :

private void WindowSelection_Load(object sender, EventArgs e)
        {
            dataGridSource = DeserializeFromXML();
            foreach (WindowHolder obj in dataGridSource)
            {
                int index = dataGridViewWindowSelection.Rows.Add();

                DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
                combo2 = (DataGridViewComboBoxColumn)dataGridViewWindowSelection.Rows[index].Cells["Reader"].OwningColumn;
                combo2.DataSource = readerSource;

                int readerSourceIndex = findReaderSourceIndex(obj.reader);
                if (readerSourceIndex != -1)
                {
                    combo2.DefaultCellStyle.NullValue = readerSource[readerSourceIndex];
                }
                else
                {
                    combo2.DefaultCellStyle.NullValue = readerSource[0];
                }

                dataGridViewWindowSelection.Rows[index].Cells["Location"].Value = obj.location;
                dataGridViewWindowSelection.Rows[index].Cells["AlwaysOnTop"].Value = obj.alwaysOnTop;
                dataGridViewWindowSelection.Rows[index].Cells["AlwaysShow"].Value = obj.alwaysShow;     
            }

        }
Community
  • 1
  • 1
Am1rr3zA
  • 7,115
  • 18
  • 83
  • 125

1 Answers1

0

Do you mean the line where you're changing combo2.DefaultCellStyle?

This happens because combo2.DefaultCellStyle is a reference to the default cell style of all the combos, so you're not changing this one combo - you're changing the common default style.

If you want the style of this combo to be different from the default one (and from the other combos' style), you should probably create a separate style and set it as the style of combo2.

I guess the statement should look something like combo2.DefaultCellStyle = ... or combo2.SetDefaultCellStyle( ... )

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141